Skip to content

Commit 028beb5

Browse files
committed
Implement MemorySpaceInterface for Clang and Target AS attributes
1 parent 0d731a7 commit 028beb5

File tree

20 files changed

+210
-98
lines changed

20 files changed

+210
-98
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
#include "clang/CIR/Dialect/IR/FPEnv.h"
2020
#include "clang/CIR/MissingFeatures.h"
2121

22+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
2223
#include "mlir/IR/Attributes.h"
2324
#include "mlir/IR/Builders.h"
2425
#include "mlir/IR/BuiltinAttributes.h"
2526
#include "mlir/IR/BuiltinOps.h"
2627
#include "mlir/IR/BuiltinTypes.h"
2728
#include "mlir/IR/Location.h"
2829
#include "mlir/IR/Types.h"
30+
#include "mlir/Support/LLVM.h"
2931
#include "llvm/ADT/APSInt.h"
3032
#include "llvm/ADT/ArrayRef.h"
3133
#include "llvm/ADT/FloatingPointMode.h"
@@ -107,34 +109,30 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
107109
}
108110

109111
/// Create a pointer type with an address space attribute.
110-
/// Either a cir::ClangAddressSpaceAttr or cir::TargetAddressSpaceAttr is
111-
/// expected.
112-
cir::PointerType getPointerTo(mlir::Type ty, mlir::Attribute addrSpaceAttr) {
113-
if (!addrSpaceAttr)
112+
cir::PointerType
113+
getPointerTo(mlir::Type ty, mlir::ptr::MemorySpaceAttrInterface memorySpace) {
114+
if (!memorySpace)
114115
return cir::PointerType::get(ty);
115-
116-
assert((mlir::isa<cir::ClangAddressSpaceAttr>(addrSpaceAttr) ||
117-
mlir::isa<cir::TargetAddressSpaceAttr>(addrSpaceAttr)) &&
118-
"expected address space attribute");
119-
120-
return cir::PointerType::get(ty, addrSpaceAttr);
116+
return cir::PointerType::get(ty, memorySpace);
121117
}
122118

123119
cir::PointerType getPointerTo(mlir::Type ty, clang::LangAS langAS) {
124120
if (langAS == clang::LangAS::Default)
125121
return getPointerTo(ty);
126122

127-
mlir::Attribute addrSpaceAttr =
123+
mlir::ptr::MemorySpaceAttrInterface addrSpaceAttr =
128124
cir::toCIRClangAddressSpaceAttr(getContext(), langAS);
125+
// Convert to MemorySpaceAttrInterface for the interface overload
129126
return getPointerTo(ty, addrSpaceAttr);
130127
}
131128

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

136-
cir::PointerType getVoidPtrTy(mlir::Attribute addrSpaceAttr) {
137-
return getPointerTo(cir::VoidType::get(getContext()), addrSpaceAttr);
133+
cir::PointerType
134+
getVoidPtrTy(mlir::ptr::MemorySpaceAttrInterface memorySpace) {
135+
return getPointerTo(cir::VoidType::get(getContext()), memorySpace);
138136
}
139137

140138
cir::MethodAttr getMethodAttr(cir::MethodType ty, cir::FuncOp methodFuncOp) {
@@ -432,7 +430,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
432430
mlir::Value createGetGlobal(mlir::Location loc, cir::GlobalOp global,
433431
bool threadLocal = false) {
434432
return cir::GetGlobalOp::create(
435-
*this, loc, getPointerTo(global.getSymType(), global.getAddrSpace()),
433+
*this, loc,
434+
getPointerTo(global.getSymType(), global.getAddrSpaceAttr()),
436435
global.getName(), threadLocal);
437436
}
438437

clang/include/clang/CIR/Dialect/IR/CIRAttrConstraints.td

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,4 @@ def CIR_AnyTBAAAttr : AnyAttrOf<[
8989
string cppType = "::mlir::Attribute";
9090
}
9191

92-
//===----------------------------------------------------------------------===//
93-
// AddressSpaceAttr constraints
94-
//===----------------------------------------------------------------------===//
95-
96-
// NOTE: We might end up using this only for GlobalOps, as we cannot apply constraints
97-
// to types.
98-
def CIR_AddressSpaceAttrConstraint
99-
: CIR_AttrConstraint<"::cir::ClangAddressSpaceAttr", "clang address space attribute">;
100-
101-
def CIR_TargetAddressSpaceAttrConstraint
102-
: CIR_AttrConstraint<"::cir::TargetAddressSpaceAttr", "target address space attribute">;
103-
104-
def CIR_AnyAddressSpaceAttr : AnyAttrOf<[
105-
CIR_AddressSpaceAttrConstraint,
106-
CIR_TargetAddressSpaceAttrConstraint
107-
]> {
108-
string cppType = "::mlir::Attribute";
109-
let constBuilderCall = "nullptr";
110-
}
111-
11292
#endif // CLANG_CIR_DIALECT_IR_CIRATTRCONSTRAINTS_TD

clang/include/clang/CIR/Dialect/IR/CIRAttrs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef CLANG_CIR_DIALECT_IR_CIRATTRS_H
1414
#define CLANG_CIR_DIALECT_IR_CIRATTRS_H
1515

16+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
1617
#include "mlir/IR/Attributes.h"
1718
#include "mlir/IR/BuiltinAttributeInterfaces.h"
1819

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define CLANG_CIR_DIALECT_IR_CIRATTRS_TD
1515

1616
include "mlir/IR/BuiltinAttributeInterfaces.td"
17+
include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.td"
1718
include "clang/CIR/Dialect/IR/CIREnumAttr.td"
1819

1920
include "clang/CIR/Dialect/IR/CIRDialect.td"
@@ -963,7 +964,9 @@ def CIR_DynamicCastInfoAttr : CIR_Attr<"DynamicCastInfo", "dyn_cast_info"> {
963964
// AddressSpaceAttr
964965
//===----------------------------------------------------------------------===//
965966

966-
def CIR_ClangAddressSpaceAttr : CIR_EnumAttr<CIR_ClangAddressSpace, "clang_address_space"> {
967+
def CIR_ClangAddressSpaceAttr : CIR_EnumAttr<CIR_ClangAddressSpace, "clang_address_space", [
968+
DeclareAttrInterfaceMethods<MemorySpaceAttrInterface>
969+
]> {
967970

968971
let summary = "Represents a language/Clang-level address space";
969972
let description = [{
@@ -1008,7 +1011,9 @@ def CIR_ClangAddressSpaceAttr : CIR_EnumAttr<CIR_ClangAddressSpace, "clang_addr
10081011
//===----------------------------------------------------------------------===//
10091012

10101013
def CIR_TargetAddressSpaceAttr : CIR_Attr< "TargetAddressSpace",
1011-
"target_address_space"> {
1014+
"target_address_space", [
1015+
DeclareAttrInterfaceMethods<MemorySpaceAttrInterface>
1016+
]> {
10121017
let summary = "Represents a target-specific numeric address space";
10131018
let description = [{
10141019
The TargetAddressSpaceAttr represents a target-specific numeric address space,

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,10 +2616,7 @@ def CIR_GlobalOp : CIR_Op<"global", [
26162616
OptionalAttr<StrAttr>:$sym_visibility,
26172617
TypeAttr:$sym_type,
26182618
CIR_GlobalLinkageKind:$linkage,
2619-
DefaultValuedAttr<
2620-
CIR_AnyAddressSpaceAttr,
2621-
"{}"
2622-
>:$addr_space,
2619+
OptionalAttr<MemorySpaceAttrInterface>:$addr_space,
26232620
OptionalAttr<CIR_TLSModel>:$tls_model,
26242621
// Note this can also be a FlatSymbolRefAttr
26252622
OptionalAttr<AnyAttr>:$initial_value,
@@ -2666,7 +2663,7 @@ def CIR_GlobalOp : CIR_Op<"global", [
26662663
// CIR defaults to external linkage.
26672664
CArg<"cir::GlobalLinkageKind",
26682665
"cir::GlobalLinkageKind::ExternalLinkage">:$linkage,
2669-
CArg<"mlir::Attribute", "{}">:$addrSpace,
2666+
CArg<"mlir::ptr::MemorySpaceAttrInterface", "{}">:$addrSpace,
26702667
CArg<"llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>",
26712668
"nullptr">:$ctorBuilder,
26722669
CArg<"llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)>",

clang/include/clang/CIR/Dialect/IR/CIRTypes.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef CLANG_CIR_DIALECT_IR_CIRTYPES_H
1414
#define CLANG_CIR_DIALECT_IR_CIRTYPES_H
1515

16+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
1617
#include "mlir/IR/BuiltinAttributes.h"
1718
#include "mlir/IR/Types.h"
1819
#include "mlir/Interfaces/DataLayoutInterfaces.h"
@@ -36,11 +37,13 @@ bool isSized(mlir::Type ty);
3637

3738
cir::ClangAddressSpace toCIRClangAddressSpace(clang::LangAS langAS);
3839

39-
/// Convert a LangAS to the appropriate address space attribute.
40-
/// Returns ClangAddressSpaceAttr for clang/language-specific address spaces,
41-
/// or TargetAddressSpaceAttr for target-specific address spaces.
42-
mlir::Attribute toCIRClangAddressSpaceAttr(mlir::MLIRContext *ctx,
43-
clang::LangAS langAS);
40+
/// Convert a LangAS to the appropriate address space attribute interface.
41+
/// Returns a MemorySpaceAttrInterface.
42+
mlir::ptr::MemorySpaceAttrInterface
43+
toCIRClangAddressSpaceAttr(mlir::MLIRContext *ctx, clang::LangAS langAS);
44+
45+
bool isSupportedCIRMemorySpaceAttr(
46+
mlir::ptr::MemorySpaceAttrInterface memorySpace);
4447

4548
constexpr unsigned getAsUnsignedValue(cir::ClangAddressSpace as) {
4649
return static_cast<unsigned>(as);

clang/include/clang/CIR/Dialect/IR/CIRTypes.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,19 @@ def CIR_PointerType : CIR_Type<"Pointer", "ptr", [
237237

238238
let parameters = (ins
239239
"mlir::Type":$pointee,
240-
OptionalParameter<"mlir::Attribute">:$addrSpace
240+
OptionalParameter<"mlir::ptr::MemorySpaceAttrInterface">:$addrSpace
241241
);
242242

243243
let skipDefaultBuilders = 1;
244244
let builders = [
245245
TypeBuilderWithInferredContext<(ins
246246
"mlir::Type":$pointee,
247-
CArg<"mlir::Attribute", "{}">:$addrSpace), [{
247+
CArg<"mlir::ptr::MemorySpaceAttrInterface", "{}">:$addrSpace), [{
248248
return $_get(pointee.getContext(), pointee, addrSpace);
249249
}]>,
250250
TypeBuilder<(ins
251251
"mlir::Type":$pointee,
252-
CArg<"mlir::Attribute", "{}">:$addrSpace), [{
252+
CArg<"mlir::ptr::MemorySpaceAttrInterface", "{}">:$addrSpace), [{
253253
return $_get($_ctxt, pointee, addrSpace);
254254
}]>
255255
];

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "clang/CIR/Dialect/IR/CIRTypes.h"
2626
#include "clang/CIR/Dialect/IR/FPEnv.h"
2727

28+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
2829
#include "mlir/IR/Attributes.h"
2930
#include "mlir/IR/Builders.h"
3031
#include "mlir/IR/BuiltinAttributes.h"
@@ -749,7 +750,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
749750
[[nodiscard]] cir::GlobalOp
750751
createGlobal(mlir::ModuleOp module, mlir::Location loc, mlir::StringRef name,
751752
mlir::Type type, bool isConst, cir::GlobalLinkageKind linkage,
752-
mlir::Attribute addrSpace = {}) {
753+
mlir::ptr::MemorySpaceAttrInterface addrSpace = {}) {
753754
mlir::OpBuilder::InsertionGuard guard(*this);
754755
setInsertionPointToStart(module.getBody());
755756
return cir::GlobalOp::create(*this, loc, name, type, isConst, linkage,
@@ -763,7 +764,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
763764
createVersionedGlobal(mlir::ModuleOp module, mlir::Location loc,
764765
mlir::StringRef name, mlir::Type type, bool isConst,
765766
cir::GlobalLinkageKind linkage,
766-
mlir::Attribute addrSpace = {}) {
767+
mlir::ptr::MemorySpaceAttrInterface addrSpace = {}) {
767768
// Create a unique name if the given name is already taken.
768769
std::string uniqueName;
769770
if (unsigned version = GlobalsVersioning[name.str()]++)

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "clang/Frontend/FrontendDiagnostic.h"
3535

3636
#include "mlir/Dialect/Func/IR/FuncOps.h"
37+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
3738
#include "mlir/IR/Attributes.h"
3839
#include "mlir/IR/BuiltinAttributes.h"
3940
#include "mlir/IR/Value.h"
@@ -1728,8 +1729,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
17281729
// the AST level this is handled within CreateTempAlloca et al., but for the
17291730
// builtin / dynamic alloca we have to handle it here.
17301731
assert(!cir::MissingFeatures::addressSpace());
1731-
mlir::Attribute AAS = getCIRAllocaAddressSpace();
1732-
mlir::Attribute EAS = cir::toCIRClangAddressSpaceAttr(
1732+
mlir::ptr::MemorySpaceAttrInterface AAS = getCIRAllocaAddressSpace();
1733+
mlir::ptr::MemorySpaceAttrInterface EAS = cir::toCIRClangAddressSpaceAttr(
17331734
&getMLIRContext(), E->getType()->getPointeeType().getAddressSpace());
17341735
if (EAS != AAS) {
17351736
assert(false && "Non-default address space for alloca NYI");

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
#include "CIRGenFunction.h"
1616
#include "CIRGenOpenMPRuntime.h"
1717
#include "EHScopeStack.h"
18+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
1819
#include "mlir/IR/Attributes.h"
1920
#include "mlir/IR/BuiltinAttributeInterfaces.h"
2021
#include "mlir/IR/BuiltinOps.h"
2122
#include "mlir/IR/SymbolTable.h"
23+
#include "mlir/Support/LLVM.h"
2224

2325
#include "clang/AST/Decl.h"
2426
#include "clang/AST/ExprCXX.h"
@@ -479,7 +481,7 @@ CIRGenModule::getOrCreateStaticVarDecl(const VarDecl &D,
479481
Name = getStaticDeclName(*this, D);
480482

481483
mlir::Type LTy = getTypes().convertTypeForMem(Ty);
482-
mlir::Attribute AS = cir::toCIRClangAddressSpaceAttr(
484+
mlir::ptr::MemorySpaceAttrInterface AS = cir::toCIRClangAddressSpaceAttr(
483485
&getMLIRContext(), getGlobalVarAddressSpace(&D));
484486

485487
// OpenCL variables in local address space and CUDA shared
@@ -595,7 +597,7 @@ cir::GlobalOp CIRGenFunction::addInitializerToStaticVarDecl(
595597
// Given those constraints, thread in the GetGlobalOp and update it
596598
// directly.
597599
GVAddr.getAddr().setType(
598-
getBuilder().getPointerTo(Init.getType(), GV.getAddrSpace()));
600+
getBuilder().getPointerTo(Init.getType(), GV.getAddrSpaceAttr()));
599601
}
600602

601603
bool NeedsDtor =

0 commit comments

Comments
 (0)