Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_CLANG_CIR_DIALECT_BUILDER_CIRBASEBUILDER_H

#include "clang/AST/CharUnits.h"
#include "clang/Basic/AddressSpaces.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
Expand Down Expand Up @@ -129,8 +130,30 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
return cir::PointerType::get(ty);
}

cir::PointerType getVoidPtrTy() {
return getPointerTo(cir::VoidType::get(getContext()));
cir::PointerType getPointerTo(mlir::Type ty, cir::TargetAddressSpaceAttr as) {
return cir::PointerType::get(ty, as);
}

cir::PointerType getPointerTo(mlir::Type ty, clang::LangAS langAS) {
if (langAS == clang::LangAS::Default) // Default address space.
return getPointerTo(ty);

if (clang::isTargetAddressSpace(langAS)) {
unsigned addrSpace = clang::toTargetAddressSpace(langAS);
auto asAttr = cir::TargetAddressSpaceAttr::get(
getContext(), getUI32IntegerAttr(addrSpace));
return getPointerTo(ty, asAttr);
}

llvm_unreachable("language-specific address spaces NYI");
}

cir::PointerType getVoidPtrTy(clang::LangAS langAS = clang::LangAS::Default) {
return getPointerTo(cir::VoidType::get(getContext()), langAS);
}

cir::PointerType getVoidPtrTy(cir::TargetAddressSpaceAttr as) {
return getPointerTo(cir::VoidType::get(getContext()), as);
}

cir::BoolAttr getCIRBoolAttr(bool state) {
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributeInterfaces.h"
#include "clang/Basic/AddressSpaces.h"

#include "clang/CIR/Dialect/IR/CIROpsEnums.h"

Expand Down
22 changes: 22 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,28 @@ def CIR_VTableAttr : CIR_Attr<"VTable", "vtable", [TypedAttrInterface]> {
}];
}

//===----------------------------------------------------------------------===//
// TargetAddressSpaceAttr
//===----------------------------------------------------------------------===//

def CIR_TargetAddressSpaceAttr : CIR_Attr< "TargetAddressSpace",
"target_address_space"> {
let summary = "Attribute representing a target-specific numeric address space";
let description = [{
Represents a target-specific numeric address space for pointer types.

Example:
```mlir
// Target-specific numeric address spaces
!cir.ptr<!s32i, addrspace(target<1>)>
!cir.ptr<!s32i, addrspace(target<10>)>
```
}];

let parameters = (ins "mlir::IntegerAttr":$value);
let assemblyFormat = "`<` `target` `<` $value `>` `>`";
}

//===----------------------------------------------------------------------===//
// ConstComplexAttr
//===----------------------------------------------------------------------===//
Expand Down
10 changes: 10 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Types.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include "clang/Basic/AddressSpaces.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
#include "clang/CIR/Interfaces/CIRTypeInterfaces.h"

namespace cir {
Expand All @@ -35,6 +38,13 @@ bool isValidFundamentalIntWidth(unsigned width);
/// void, or abstract types.
bool isSized(mlir::Type ty);

//===----------------------------------------------------------------------===//
// AddressSpace helpers
//===----------------------------------------------------------------------===//

/// Returns the integer value of a CIR address space for LLVM.
unsigned getTargetAddrSpaceFromAttr(cir::TargetAddressSpaceAttr attr);

} // namespace cir

//===----------------------------------------------------------------------===//
Expand Down
57 changes: 43 additions & 14 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
#define CLANG_CIR_DIALECT_IR_CIRTYPES_TD

include "clang/CIR/Dialect/IR/CIRDialect.td"
include "clang/CIR/Dialect/IR/CIREnumAttr.td"
include "clang/CIR/Dialect/IR/CIRTypeConstraints.td"
include "clang/CIR/Interfaces/CIRTypeInterfaces.td"
include "mlir/Interfaces/DataLayoutInterfaces.td"
include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/EnumAttr.td"

//===----------------------------------------------------------------------===//
// CIR Types
Expand Down Expand Up @@ -226,32 +228,59 @@ def CIR_PointerType : CIR_Type<"Pointer", "ptr", [
]> {
let summary = "CIR pointer type";
let description = [{
The `!cir.ptr` type represents C and C++ pointer types and C++ reference
types, other than pointers-to-members. The `pointee` type is the type
pointed to.
The `!cir.ptr` type is a typed pointer type. It is used to represent
pointers to objects in C/C++. The type of the pointed-to object is given by
the `pointee` parameter. The `addrSpace` parameter is an optional address
space attribute that specifies the address space of the pointer. If not
specified, the pointer is assumed to be in the default address space.

TODO(CIR): The address space attribute is not yet implemented.
The `!cir.ptr` type can point to any type, including fundamental types,
records, arrays, vectors, functions, and other pointers. It can also point
to incomplete types, such as incomplete records.

Note: Data-member pointers and method pointers are represented by
`!cir.data_member` and `!cir.method` types, respectively not by
`!cir.ptr` type.

Examples:

```mlir
!cir.ptr<!cir.int<u, 8>>
!cir.ptr<!cir.float>
!cir.ptr<!cir.record<struct "MyStruct">>
!cir.ptr<!cir.int<u, 8>, target_address_space(1)>
!cir.ptr<!cir.record<struct "MyStruct">, target_address_space(5)>
```
}];

let parameters = (ins "mlir::Type":$pointee);
let parameters = (ins
"mlir::Type":$pointee,
OptionalParameter<
"cir::TargetAddressSpaceAttr"
>:$addrSpace
);

let skipDefaultBuilders = 1;
let builders = [
TypeBuilderWithInferredContext<(ins "mlir::Type":$pointee), [{
return $_get(pointee.getContext(), pointee);
TypeBuilderWithInferredContext<(ins
"mlir::Type":$pointee,
CArg<"cir::TargetAddressSpaceAttr", "nullptr">:$addrSpace), [{
return $_get(pointee.getContext(), pointee, addrSpace);
}]>,
TypeBuilder<(ins "mlir::Type":$pointee), [{
return $_get($_ctxt, pointee);
TypeBuilder<(ins
"mlir::Type":$pointee,
CArg<"cir::TargetAddressSpaceAttr", "nullptr">:$addrSpace), [{
return $_get($_ctxt, pointee, addrSpace);
}]>
];

let assemblyFormat = [{
`<` $pointee `>`
`<`
$pointee
( `,` ` ` custom<TargetAddressSpace>($addrSpace)^ )?
`>`
}];

let genVerifyDecl = 1;

let skipDefaultBuilders = 1;

let extraClassDeclaration = [{
template <typename ...Types>
bool isPtrTo() const {
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2052,8 +2052,8 @@ mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
// CIR uses its own alloca address space rather than follow the target data
// layout like original CodeGen. The data layout awareness should be done in
// the lowering pass instead.
assert(!cir::MissingFeatures::addressSpace());
cir::PointerType localVarPtrTy = builder.getPointerTo(ty);
cir::PointerType localVarPtrTy =
builder.getPointerTo(ty, getASTAllocaAddressSpace());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
builder.getPointerTo(ty, getASTAllocaAddressSpace());
builder.getPointerTo(ty, getCIRAllocaAddressSpace());

mlir::IntegerAttr alignIntAttr = cgm.getSize(alignment);

mlir::Value addr;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
SInt128Ty = cir::IntType::get(&getMLIRContext(), 128, /*isSigned=*/true);
UInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/false);
UInt8PtrTy = cir::PointerType::get(UInt8Ty);
ASTAllocaAddressSpace = getTargetCIRGenInfo().getASTAllocaAddressSpace();
UInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/false);
UInt32Ty = cir::IntType::get(&getMLIRContext(), 32, /*isSigned=*/false);
UInt64Ty = cir::IntType::get(&getMLIRContext(), 64, /*isSigned=*/false);
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenTypeCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define LLVM_CLANG_LIB_CIR_CIRGENTYPECACHE_H

#include "clang/AST/CharUnits.h"
#include "clang/Basic/AddressSpaces.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"

namespace clang::CIRGen {
Expand Down Expand Up @@ -73,13 +74,17 @@ struct CIRGenTypeCache {
/// The alignment of size_t.
unsigned char SizeAlignInBytes;

LangAS ASTAllocaAddressSpace;

clang::CharUnits getSizeAlign() const {
return clang::CharUnits::fromQuantity(SizeAlignInBytes);
}

clang::CharUnits getPointerAlign() const {
return clang::CharUnits::fromQuantity(PointerAlignInBytes);
}

LangAS getASTAllocaAddressSpace() const { return ASTAllocaAddressSpace; }
};

} // namespace clang::CIRGen
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ mlir::Type CIRGenTypes::convertType(QualType type) {

mlir::Type pointeeType = convertType(elemTy);

resultType = builder.getPointerTo(pointeeType);
resultType = builder.getPointerTo(pointeeType, elemTy.getAddressSpace());
break;
}

Expand Down
4 changes: 4 additions & 0 deletions clang/lib/CIR/CodeGen/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "ABIInfo.h"
#include "CIRGenTypes.h"
#include "clang/Basic/AddressSpaces.h"

#include <memory>
#include <utility>
Expand Down Expand Up @@ -43,6 +44,9 @@ class TargetCIRGenInfo {
/// Returns ABI info helper for the target.
const ABIInfo &getABIInfo() const { return *info; }

/// Get the AST address space for alloca.
virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }

/// Determine whether a call to an unprototyped functions under
/// the given calling convention should use the variadic
/// convention or the non-variadic convention.
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ parseFloatLiteral(mlir::AsmParser &parser,
mlir::FailureOr<llvm::APFloat> &value,
cir::FPTypeInterface fpType);

//===----------------------------------------------------------------------===//
// AddressSpaceAttr
//===----------------------------------------------------------------------===//

mlir::ParseResult
parseTargetAddressSpace(mlir::AsmParser &p, cir::TargetAddressSpaceAttr &attr);

void printTargetAddressSpace(mlir::AsmPrinter &p,
cir::TargetAddressSpaceAttr attr);

static mlir::ParseResult parseConstPtr(mlir::AsmParser &parser,
mlir::IntegerAttr &value);

Expand Down
85 changes: 69 additions & 16 deletions clang/lib/CIR/Dialect/IR/CIRTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "clang/CIR/Dialect/IR/CIRTypes.h"

#include "mlir/IR/DialectImplementation.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypesDetails.h"
#include "clang/CIR/MissingFeatures.h"
Expand All @@ -38,6 +39,27 @@ parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
static void printFuncTypeParams(mlir::AsmPrinter &p,
mlir::ArrayRef<mlir::Type> params,
bool isVarArg);
//===----------------------------------------------------------------------===//
// CIR Custom Parser/Printer Signatures
//===----------------------------------------------------------------------===//

static mlir::ParseResult
parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
bool &isVarArg);

static void printFuncTypeParams(mlir::AsmPrinter &p,
mlir::ArrayRef<mlir::Type> params,
bool isVarArg);

//===----------------------------------------------------------------------===//
// AddressSpace
//===----------------------------------------------------------------------===//

mlir::ParseResult parseTargetAddressSpace(mlir::AsmParser &p,
cir::TargetAddressSpaceAttr &attr);

void printTargetAddressSpace(mlir::AsmPrinter &p,
cir::TargetAddressSpaceAttr attr);

//===----------------------------------------------------------------------===//
// Get autogenerated stuff
Expand Down Expand Up @@ -297,6 +319,20 @@ bool RecordType::isLayoutIdentical(const RecordType &other) {
// Data Layout information for types
//===----------------------------------------------------------------------===//

llvm::TypeSize
PointerType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// FIXME: improve this in face of address spaces
return llvm::TypeSize::getFixed(64);
}

uint64_t
PointerType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// FIXME: improve this in face of address spaces
return 8;
}

llvm::TypeSize
RecordType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
Expand Down Expand Up @@ -766,30 +802,47 @@ mlir::LogicalResult cir::VectorType::verify(
}

//===----------------------------------------------------------------------===//
// PointerType Definitions
// TargetAddressSpace definitions
//===----------------------------------------------------------------------===//

llvm::TypeSize
PointerType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// FIXME: improve this in face of address spaces
return llvm::TypeSize::getFixed(64);
// Convert from TargetAddressSpaceAttr to the actual integer address space.
unsigned cir::getTargetAddrSpaceFromAttr(cir::TargetAddressSpaceAttr attr) {
if (!attr)
return 0; // Default address space is 0 in LLVM.
return attr.getValue().getUInt();
}

uint64_t
PointerType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// FIXME: improve this in face of address spaces
return 8;
}
mlir::ParseResult parseTargetAddressSpace(mlir::AsmParser &p,
cir::TargetAddressSpaceAttr &attr) {
if (failed(p.parseKeyword("target_address_space")))
return mlir::failure();

mlir::LogicalResult
PointerType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
mlir::Type pointee) {
// TODO(CIR): Verification of the address space goes here.
if (failed(p.parseLParen()))
return mlir::failure();

int64_t targetValue;
if (failed(p.parseInteger(targetValue)))
return p.emitError(p.getCurrentLocation(),
"expected integer address space value");

if (failed(p.parseRParen()))
return p.emitError(p.getCurrentLocation(),
"expected ')' after address space value");

mlir::MLIRContext *context = p.getBuilder().getContext();
auto intTy = mlir::IntegerType::get(context, 32);
attr = cir::TargetAddressSpaceAttr::get(
context, mlir::IntegerAttr::get(intTy, targetValue));
return mlir::success();
}

// The custom printer for the `addrspace` parameter in `!cir.ptr`.
// in the format of `target_address_space(N)`.
void printTargetAddressSpace(mlir::AsmPrinter &p,
cir::TargetAddressSpaceAttr attr) {
p << "target_address_space(" << attr.getValue().getUInt() << ")";
}

//===----------------------------------------------------------------------===//
// CIR Dialect
//===----------------------------------------------------------------------===//
Expand Down
Loading
Loading