Skip to content

Commit 8936618

Browse files
committed
Refactor address space handling to use TargetAddressSpaceAttr and update related functionality
1 parent dce6d61 commit 8936618

File tree

14 files changed

+95
-250
lines changed

14 files changed

+95
-250
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_CLANG_CIR_DIALECT_BUILDER_CIRBASEBUILDER_H
1111

1212
#include "clang/AST/CharUnits.h"
13+
#include "clang/Basic/AddressSpaces.h"
1314
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
1415
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1516
#include "clang/CIR/Dialect/IR/CIRTypes.h"
@@ -129,19 +130,29 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
129130
return cir::PointerType::get(ty);
130131
}
131132

132-
cir::PointerType getPointerTo(mlir::Type ty, cir::AddressSpace as) {
133+
cir::PointerType getPointerTo(mlir::Type ty, cir::TargetAddressSpaceAttr as) {
133134
return cir::PointerType::get(ty, as);
134135
}
135136

136137
cir::PointerType getPointerTo(mlir::Type ty, clang::LangAS langAS) {
137-
return getPointerTo(ty, cir::toCIRAddressSpace(langAS));
138+
if (langAS == clang::LangAS::Default) // Default address space.
139+
return getPointerTo(ty);
140+
141+
if (clang::isTargetAddressSpace(langAS)) {
142+
unsigned addrSpace = clang::toTargetAddressSpace(langAS);
143+
auto asAttr = cir::TargetAddressSpaceAttr::get(
144+
getContext(), getUI32IntegerAttr(addrSpace));
145+
return getPointerTo(ty, asAttr);
146+
}
147+
148+
llvm_unreachable("language-specific address spaces NYI");
138149
}
139150

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

144-
cir::PointerType getVoidPtrTy(cir::AddressSpace as) {
155+
cir::PointerType getVoidPtrTy(cir::TargetAddressSpaceAttr as) {
145156
return getPointerTo(cir::VoidType::get(getContext()), as);
146157
}
147158

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

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -602,78 +602,25 @@ def CIR_VTableAttr : CIR_Attr<"VTable", "vtable", [TypedAttrInterface]> {
602602
}
603603

604604
//===----------------------------------------------------------------------===//
605-
// AddressSpaceAttr
605+
// TargetAddressSpaceAttr
606606
//===----------------------------------------------------------------------===//
607607

608-
def CIR_AddressSpaceAttr : CIR_EnumAttr<CIR_AddressSpace, "address_space"> {
609-
let summary = "Attribute representing memory address spaces";
608+
def CIR_TargetAddressSpaceAttr : CIR_Attr< "TargetAddressSpace",
609+
"target_address_space"> {
610+
let summary = "Attribute representing a target-specific numeric address space";
610611
let description = [{
611-
Represents different memory address spaces for pointer types.
612-
Address spaces distinguish between different types of memory regions,
613-
such as global, local, or constant memory.
612+
Represents a target-specific numeric address space for pointer types.
614613

615-
The `value` parameter is an extensible enum, which encodes target address
616-
space as an offset to the last language address space. For that reason, the
617-
attribute is implemented as custom AddressSpaceAttr, which provides custom
618-
printer and parser for the `value` parameter.
619-
620-
CIR supports two categories:
621-
- Language address spaces: Predefined spaces like `offload_global`,
622-
`offload_constant`, `offload_private` for offload programming models
623-
- Target address spaces: Numeric spaces `target<N>` for target-specific
624-
memory regions, where N is the address space number
625-
626-
Examples:
614+
Example:
627615
```mlir
628-
// Default address space (implicit)
629-
!cir.ptr<!s32i>
630-
631-
// Language-defined offload address spaces
632-
!cir.ptr<!s32i, addrspace(offload_global)>
633-
!cir.ptr<!s32i, addrspace(offload_constant)>
634-
!cir.ptr<!s32i, addrspace(offload_private)>
635-
636616
// Target-specific numeric address spaces
637617
!cir.ptr<!s32i, addrspace(target<1>)>
638618
!cir.ptr<!s32i, addrspace(target<10>)>
639619
```
640620
}];
641-
let builders = [
642-
AttrBuilder<(ins "clang::LangAS":$langAS), [{
643-
return $_get($_ctxt, cir::toCIRAddressSpace(langAS));
644-
}]>
645-
];
646-
647-
let assemblyFormat = [{
648-
`(` custom<AddressSpaceValue>($value) `)`
649-
}];
650-
651-
let defaultValue = "cir::AddressSpace::Default";
652-
653-
let extraClassDeclaration = [{
654-
bool isLang() const;
655-
bool isTarget() const;
656-
unsigned getTargetValue() const;
657-
unsigned getAsUnsignedValue() const;
658-
}];
659621

660-
let extraClassDefinition = [{
661-
unsigned $cppClass::getAsUnsignedValue() const {
662-
return static_cast<unsigned>(getValue());
663-
}
664-
665-
bool $cppClass::isLang() const {
666-
return cir::isLangAddressSpace(getValue());
667-
}
668-
669-
bool $cppClass::isTarget() const {
670-
return cir::isTargetAddressSpace(getValue());
671-
}
672-
673-
unsigned $cppClass::getTargetValue() const {
674-
return cir::getTargetAddressSpaceValue(getValue());
675-
}
676-
}];
622+
let parameters = (ins "mlir::IntegerAttr":$value);
623+
let assemblyFormat = "`<` `target` `<` $value `>` `>`";
677624
}
678625

679626
//===----------------------------------------------------------------------===//

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,4 @@ class CIR_DefaultValuedEnumParameter<EnumAttrInfo info, string value = "">
3535
let defaultValue = value;
3636
}
3737

38-
def CIR_AddressSpace : CIR_I32EnumAttr<
39-
"AddressSpace", "address space kind", [
40-
I32EnumAttrCase<"Default", 0, "default">,
41-
I32EnumAttrCase<"OffloadPrivate", 1, "offload_private">,
42-
I32EnumAttrCase<"OffloadLocal", 2, "offload_local">,
43-
I32EnumAttrCase<"OffloadGlobal", 3, "offload_global">,
44-
I32EnumAttrCase<"OffloadConstant", 4, "offload_constant">,
45-
I32EnumAttrCase<"OffloadGeneric", 5, "offload_generic">,
46-
I32EnumAttrCase<"Target", 6, "target">
47-
]> {
48-
let description = [{
49-
The `address_space` attribute is used to represent address spaces for
50-
pointer types in CIR. It provides a unified model on top of `clang::LangAS`
51-
and simplifies the representation of address spaces.
52-
}];
53-
54-
let genSpecializedAttr = 0;
55-
}
56-
5738
#endif // CLANG_CIR_DIALECT_IR_CIRENUMATTR_TD

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

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "mlir/IR/Types.h"
1818
#include "mlir/Interfaces/DataLayoutInterfaces.h"
1919
#include "clang/Basic/AddressSpaces.h"
20+
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
2021
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
2122
#include "clang/CIR/Interfaces/CIRTypeInterfaces.h"
2223

@@ -41,38 +42,9 @@ bool isSized(mlir::Type ty);
4142
// AddressSpace helpers
4243
//===----------------------------------------------------------------------===//
4344

44-
cir::AddressSpace toCIRAddressSpace(clang::LangAS langAS);
45+
/// Returns the integer value of a CIR address space for LLVM.
46+
unsigned getTargetAddrSpaceFromAttr(cir::TargetAddressSpaceAttr attr);
4547

46-
constexpr unsigned getAsUnsignedValue(cir::AddressSpace as) {
47-
return static_cast<unsigned>(as);
48-
}
49-
50-
inline constexpr unsigned targetAddressSpaceOffset =
51-
cir::getMaxEnumValForAddressSpace();
52-
53-
// Target address space is used for target-specific address spaces that are not
54-
// part of the enum. Its value is represented as an offset from the maximum
55-
// value of the enum. Make sure that it is always the last enum value.
56-
static_assert(getAsUnsignedValue(cir::AddressSpace::Target) ==
57-
cir::getMaxEnumValForAddressSpace(),
58-
"Target address space must be the last enum value");
59-
60-
constexpr bool isTargetAddressSpace(cir::AddressSpace as) {
61-
return getAsUnsignedValue(as) >= cir::getMaxEnumValForAddressSpace();
62-
}
63-
64-
constexpr bool isLangAddressSpace(cir::AddressSpace as) {
65-
return !isTargetAddressSpace(as);
66-
}
67-
68-
constexpr unsigned getTargetAddressSpaceValue(cir::AddressSpace as) {
69-
assert(isTargetAddressSpace(as) && "expected target address space");
70-
return getAsUnsignedValue(as) - targetAddressSpaceOffset;
71-
}
72-
73-
constexpr cir::AddressSpace computeTargetAddressSpace(unsigned v) {
74-
return static_cast<cir::AddressSpace>(v + targetAddressSpaceOffset);
75-
}
7648
} // namespace cir
7749

7850
//===----------------------------------------------------------------------===//

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,37 +248,36 @@ def CIR_PointerType : CIR_Type<"Pointer", "ptr", [
248248
!cir.ptr<!cir.int<u, 8>>
249249
!cir.ptr<!cir.float>
250250
!cir.ptr<!cir.record<struct "MyStruct">>
251-
!cir.ptr<!cir.record<struct "MyStruct">, addrspace(offload_private)>
252-
!cir.ptr<!cir.int<u, 8>, addrspace(target<1>)>
251+
!cir.ptr<!cir.int<u, 8>, target_address_space(1)>
252+
!cir.ptr<!cir.record<struct "MyStruct">, target_address_space(5)>
253253
```
254254
}];
255255

256256
let parameters = (ins
257257
"mlir::Type":$pointee,
258-
CIR_DefaultValuedEnumParameter<
259-
CIR_AddressSpace,
260-
"cir::AddressSpace::Default"
258+
OptionalParameter<
259+
"cir::TargetAddressSpaceAttr"
261260
>:$addrSpace
262261
);
263262

264263
let skipDefaultBuilders = 1;
265264
let builders = [
266265
TypeBuilderWithInferredContext<(ins
267266
"mlir::Type":$pointee,
268-
CArg<"cir::AddressSpace", "cir::AddressSpace::Default">:$addrSpace), [{
267+
CArg<"cir::TargetAddressSpaceAttr", "nullptr">:$addrSpace), [{
269268
return $_get(pointee.getContext(), pointee, addrSpace);
270269
}]>,
271270
TypeBuilder<(ins
272271
"mlir::Type":$pointee,
273-
CArg<"cir::AddressSpace", "cir::AddressSpace::Default">:$addrSpace), [{
272+
CArg<"cir::TargetAddressSpaceAttr", "nullptr">:$addrSpace), [{
274273
return $_get($_ctxt, pointee, addrSpace);
275274
}]>
276275
];
277276

278277
let assemblyFormat = [{
279278
`<`
280279
$pointee
281-
( `,` `addrspace` `(` `` custom<AddressSpaceValue>($addrSpace)^ `)` )?
280+
( `,` ` ` custom<TargetAddressSpace>($addrSpace)^ )?
282281
`>`
283282
}];
284283

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
20532053
// layout like original CodeGen. The data layout awareness should be done in
20542054
// the lowering pass instead.
20552055
cir::PointerType localVarPtrTy =
2056-
builder.getPointerTo(ty, getCIRAllocaAddressSpace());
2056+
builder.getPointerTo(ty, getASTAllocaAddressSpace());
20572057
mlir::IntegerAttr alignIntAttr = cgm.getSize(alignment);
20582058

20592059
mlir::Value addr;

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
7676
SInt128Ty = cir::IntType::get(&getMLIRContext(), 128, /*isSigned=*/true);
7777
UInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/false);
7878
UInt8PtrTy = cir::PointerType::get(UInt8Ty);
79-
cirAllocaAddressSpace = getTargetCIRGenInfo().getCIRAllocaAddressSpace();
79+
ASTAllocaAddressSpace = getTargetCIRGenInfo().getASTAllocaAddressSpace();
8080
UInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/false);
8181
UInt32Ty = cir::IntType::get(&getMLIRContext(), 32, /*isSigned=*/false);
8282
UInt64Ty = cir::IntType::get(&getMLIRContext(), 64, /*isSigned=*/false);

clang/lib/CIR/CodeGen/CIRGenTypeCache.h

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

1616
#include "clang/AST/CharUnits.h"
17+
#include "clang/Basic/AddressSpaces.h"
1718
#include "clang/CIR/Dialect/IR/CIRTypes.h"
1819

1920
namespace clang::CIRGen {
@@ -73,7 +74,7 @@ struct CIRGenTypeCache {
7374
/// The alignment of size_t.
7475
unsigned char SizeAlignInBytes;
7576

76-
cir::AddressSpace cirAllocaAddressSpace;
77+
LangAS ASTAllocaAddressSpace;
7778

7879
clang::CharUnits getSizeAlign() const {
7980
return clang::CharUnits::fromQuantity(SizeAlignInBytes);
@@ -83,9 +84,7 @@ struct CIRGenTypeCache {
8384
return clang::CharUnits::fromQuantity(PointerAlignInBytes);
8485
}
8586

86-
cir::AddressSpace getCIRAllocaAddressSpace() const {
87-
return cirAllocaAddressSpace;
88-
}
87+
LangAS getASTAllocaAddressSpace() const { return ASTAllocaAddressSpace; }
8988
};
9089

9190
} // namespace clang::CIRGen

clang/lib/CIR/CodeGen/TargetInfo.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "ABIInfo.h"
1818
#include "CIRGenTypes.h"
19+
#include "clang/Basic/AddressSpaces.h"
1920

2021
#include <memory>
2122
#include <utility>
@@ -43,10 +44,8 @@ class TargetCIRGenInfo {
4344
/// Returns ABI info helper for the target.
4445
const ABIInfo &getABIInfo() const { return *info; }
4546

46-
/// Get the CIR address space for alloca.
47-
virtual cir::AddressSpace getCIRAllocaAddressSpace() const {
48-
return cir::AddressSpace::Default;
49-
}
47+
/// Get the AST address space for alloca.
48+
virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
5049

5150
/// Determine whether a call to an unprototyped functions under
5251
/// the given calling convention should use the variadic

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ parseFloatLiteral(mlir::AsmParser &parser,
4747
// AddressSpaceAttr
4848
//===----------------------------------------------------------------------===//
4949

50-
mlir::ParseResult parseAddressSpaceValue(mlir::AsmParser &p,
51-
cir::AddressSpace &addrSpace);
50+
mlir::ParseResult
51+
parseTargetAddressSpace(mlir::AsmParser &p, cir::TargetAddressSpaceAttr &attr);
5252

53-
void printAddressSpaceValue(mlir::AsmPrinter &p, cir::AddressSpace addrSpace);
53+
void printTargetAddressSpace(mlir::AsmPrinter &p,
54+
cir::TargetAddressSpaceAttr attr);
5455

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

0 commit comments

Comments
 (0)