CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
184 lines
6.0 KiB
C++
184 lines
6.0 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This class provides a simple wrapper for a pair of a pointer and an
|
|
// alignment.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef CLANG_LIB_CIR_ADDRESS_H
|
|
#define CLANG_LIB_CIR_ADDRESS_H
|
|
|
|
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
|
|
#include "mlir/IR/Value.h"
|
|
#include "clang/AST/CharUnits.h"
|
|
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
|
|
#include "clang/CIR/Dialect/IR/CIRDialect.h"
|
|
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
|
|
#include "clang/CIR/Dialect/IR/CIRTypes.h"
|
|
#include "clang/CIR/MissingFeatures.h"
|
|
#include "llvm/ADT/PointerIntPair.h"
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
namespace clang::CIRGen {
|
|
|
|
// Forward declaration to avoid a circular dependency
|
|
class CIRGenBuilderTy;
|
|
|
|
class Address {
|
|
|
|
// The boolean flag indicates whether the pointer is known to be non-null.
|
|
llvm::PointerIntPair<mlir::Value, 1, bool> pointerAndKnownNonNull;
|
|
|
|
/// The expected CIR type of the pointer. Carrying accurate element type
|
|
/// information in Address makes it more convenient to work with Address
|
|
/// values and allows frontend assertions to catch simple mistakes.
|
|
mlir::Type elementType;
|
|
|
|
clang::CharUnits alignment;
|
|
|
|
protected:
|
|
Address(std::nullptr_t) : elementType(nullptr) {}
|
|
|
|
public:
|
|
Address(mlir::Value pointer, mlir::Type elementType,
|
|
clang::CharUnits alignment)
|
|
: Address(pointer, elementType, alignment, false) {}
|
|
|
|
Address(mlir::Value pointer, mlir::Type elementType,
|
|
clang::CharUnits alignment, bool isKnownNonNull)
|
|
: pointerAndKnownNonNull(pointer, isKnownNonNull),
|
|
elementType(elementType), alignment(alignment) {
|
|
assert(pointer && "Pointer cannot be null");
|
|
assert(elementType && "Element type cannot be null");
|
|
assert(!alignment.isZero() && "Alignment cannot be zero");
|
|
|
|
assert(mlir::isa<cir::PointerType>(pointer.getType()) &&
|
|
"Expected cir.ptr type");
|
|
|
|
assert(mlir::cast<cir::PointerType>(pointer.getType()).getPointee() ==
|
|
elementType);
|
|
}
|
|
|
|
Address(mlir::Value pointer, clang::CharUnits alignment)
|
|
: Address(pointer,
|
|
mlir::cast<cir::PointerType>(pointer.getType()).getPointee(),
|
|
alignment) {
|
|
assert((!alignment.isZero() || pointer == nullptr) &&
|
|
"creating valid address with invalid alignment");
|
|
}
|
|
|
|
static Address invalid() { return Address(nullptr); }
|
|
bool isValid() const {
|
|
return pointerAndKnownNonNull.getPointer() != nullptr;
|
|
}
|
|
|
|
/// Return address with different pointer, but same element type and
|
|
/// alignment.
|
|
Address withPointer(mlir::Value newPtr) const {
|
|
return Address(newPtr, getElementType(), getAlignment());
|
|
}
|
|
|
|
/// Return address with different alignment, but same pointer and element
|
|
/// type.
|
|
Address withAlignment(clang::CharUnits newAlignment) const {
|
|
return Address(getPointer(), getElementType(), newAlignment,
|
|
isKnownNonNull());
|
|
}
|
|
|
|
/// Return address with different element type, a bitcast pointer, and
|
|
/// the same alignment.
|
|
Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const;
|
|
|
|
mlir::Value getPointer() const {
|
|
assert(isValid());
|
|
return pointerAndKnownNonNull.getPointer();
|
|
}
|
|
|
|
mlir::Value getBasePointer() const {
|
|
// TODO(cir): Remove the version above when we catchup with OG codegen on
|
|
// ptr auth.
|
|
assert(isValid() && "pointer isn't valid");
|
|
return getPointer();
|
|
}
|
|
|
|
/// Return the pointer contained in this class after authenticating it and
|
|
/// adding offset to it if necessary.
|
|
mlir::Value emitRawPointer() const {
|
|
assert(!cir::MissingFeatures::addressPointerAuthInfo());
|
|
return getBasePointer();
|
|
}
|
|
|
|
mlir::Type getType() const {
|
|
assert(mlir::cast<cir::PointerType>(
|
|
pointerAndKnownNonNull.getPointer().getType())
|
|
.getPointee() == elementType);
|
|
|
|
return mlir::cast<cir::PointerType>(getPointer().getType());
|
|
}
|
|
|
|
mlir::Type getElementType() const {
|
|
assert(isValid());
|
|
assert(mlir::cast<cir::PointerType>(
|
|
pointerAndKnownNonNull.getPointer().getType())
|
|
.getPointee() == elementType);
|
|
return elementType;
|
|
}
|
|
|
|
mlir::ptr::MemorySpaceAttrInterface getAddressSpace() const {
|
|
auto ptrTy = mlir::dyn_cast<cir::PointerType>(getType());
|
|
return ptrTy.getAddrSpace();
|
|
}
|
|
|
|
clang::CharUnits getAlignment() const { return alignment; }
|
|
|
|
/// Get the operation which defines this address.
|
|
mlir::Operation *getDefiningOp() const {
|
|
if (!isValid())
|
|
return nullptr;
|
|
return getPointer().getDefiningOp();
|
|
}
|
|
|
|
template <typename OpTy> OpTy getDefiningOp() const {
|
|
return mlir::dyn_cast_or_null<OpTy>(getDefiningOp());
|
|
}
|
|
|
|
/// Return the underlying alloca for this address, if any.
|
|
///
|
|
/// Addresses may refer to an alloca through an address space cast, for
|
|
/// example when a target stack address space is cast to the language-visible
|
|
/// address space. Peel those casts so callers that need to annotate the
|
|
/// original alloca can still find it.
|
|
cir::AllocaOp getUnderlyingAllocaOp() const {
|
|
mlir::Value ptr = getPointer();
|
|
while (cir::CastOp castOp = ptr.getDefiningOp<cir::CastOp>()) {
|
|
if (!castOp.isAllocaPreservingCast())
|
|
break;
|
|
ptr = castOp.getSrc();
|
|
}
|
|
return ptr.getDefiningOp<cir::AllocaOp>();
|
|
}
|
|
|
|
/// Whether the pointer is known not to be null.
|
|
bool isKnownNonNull() const {
|
|
assert(isValid() && "Invalid address");
|
|
return static_cast<bool>(pointerAndKnownNonNull.getInt());
|
|
}
|
|
|
|
/// Set the non-null bit.
|
|
Address setKnownNonNull() {
|
|
assert(isValid() && "Invalid address");
|
|
pointerAndKnownNonNull.setInt(true);
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
} // namespace clang::CIRGen
|
|
|
|
#endif // CLANG_LIB_CIR_ADDRESS_H
|