Skip to content

Commit a6c0bea

Browse files
committed
Handle hybrid AS approach with GlobalOps
1 parent c959448 commit a6c0bea

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def CIR_AnyAddressSpaceAttr : AnyAttrOf<[
106106
CIR_TargetAddressSpaceAttrConstraint
107107
]> {
108108
string cppType = "::mlir::Attribute";
109+
let constBuilderCall = "nullptr";
109110
}
110111

111112
#endif // CLANG_CIR_DIALECT_IR_CIRATTRCONSTRAINTS_TD

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,8 +2617,8 @@ def CIR_GlobalOp : CIR_Op<"global", [
26172617
TypeAttr:$sym_type,
26182618
CIR_GlobalLinkageKind:$linkage,
26192619
DefaultValuedAttr<
2620-
CIR_AddressSpaceAttr,
2621-
"AddressSpace::Default"
2620+
CIR_AnyAddressSpaceAttr,
2621+
"{}"
26222622
>:$addr_space,
26232623
OptionalAttr<CIR_TLSModel>:$tls_model,
26242624
// Note this can also be a FlatSymbolRefAttr
@@ -2642,7 +2642,7 @@ def CIR_GlobalOp : CIR_Op<"global", [
26422642
(`comdat` $comdat^)?
26432643
($tls_model^)?
26442644
(`dso_local` $dso_local^)?
2645-
(`addrspace` `` $addr_space^)?
2645+
( `,` ` ` custom<AddressSpaceValue>($addr_space)^ )?
26462646
$sym_name
26472647
custom<GlobalOpTypeAndInitialValue>($sym_type, $initial_value, $ctorRegion, $dtorRegion)
26482648
($annotations^)?

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,9 +1112,9 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
11121112
entry = dyn_cast_or_null<cir::GlobalOp>(v);
11131113
}
11141114

1115-
cir::AddressSpace cirAS = cir::toCIRAddressSpace(langAS);
1115+
mlir::Attribute cirAS = cir::toCIRAddressSpaceAttr(&getMLIRContext(), langAS);
11161116
if (entry) {
1117-
cir::AddressSpace entryCIRAS = entry.getAddrSpace();
1117+
mlir::Attribute entryCIRAS = entry.getAddrSpace();
11181118
if (WeakRefReferences.erase(entry)) {
11191119
if (d && !d->hasAttr<WeakAttr>()) {
11201120
auto lt = cir::GlobalLinkageKind::ExternalLinkage;

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "mlir/Dialect/Func/IR/FuncOps.h"
3030
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
31+
#include "mlir/IR/Attributes.h"
3132
#include "mlir/IR/Builders.h"
3233
#include "mlir/IR/BuiltinAttributes.h"
3334
#include "mlir/IR/BuiltinTypes.h"
@@ -55,6 +56,8 @@ using namespace mlir;
5556
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
5657
#include "clang/CIR/Interfaces/CIROpInterfaces.h"
5758
#include <clang/CIR/MissingFeatures.h>
59+
#include <clang/CIR/Dialect/IR/CIRTypes.h>
60+
#include <clang/CIR/Dialect/IR/CIRDialect.h>
5861

5962
//===----------------------------------------------------------------------===//
6063
// CIR Dialect
@@ -304,6 +307,12 @@ static void printOmittedTerminatorRegion(mlir::OpAsmPrinter &printer,
304307
}
305308
}
306309

310+
mlir::ParseResult parseAddressSpaceValue(mlir::AsmParser &p,
311+
mlir::Attribute &attr);
312+
313+
void printAddressSpaceValue(mlir::AsmPrinter &printer, cir::GlobalOp op,
314+
mlir::Attribute attr);
315+
307316
//===----------------------------------------------------------------------===//
308317
// AllocaOp
309318
//===----------------------------------------------------------------------===//
@@ -2481,17 +2490,7 @@ cir::GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
24812490
if (auto g = dyn_cast<GlobalOp>(op)) {
24822491
symTy = g.getSymType();
24832492
// Convert enum to attribute for comparison
2484-
cir::AddressSpace symAddrSpace = g.getAddrSpace();
2485-
if (symAddrSpace == cir::AddressSpace::Default) {
2486-
symAddrSpaceAttr = {};
2487-
} else if (cir::isTargetAddressSpace(symAddrSpace)) {
2488-
unsigned targetAS =
2489-
cir::getTargetAddressSpaceValueFromCIRAS(symAddrSpace);
2490-
symAddrSpaceAttr =
2491-
cir::TargetAddressSpaceAttr::get(getContext(), targetAS);
2492-
} else {
2493-
symAddrSpaceAttr = cir::AddressSpaceAttr::get(getContext(), symAddrSpace);
2494-
}
2493+
symAddrSpaceAttr = g.getAddrSpace();
24952494
// Verify that for thread local global access, the global needs to
24962495
// be marked with tls bits.
24972496
if (getTls() && !g.getTlsModel())

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,11 @@ void printAddressSpaceValue(mlir::AsmPrinter &p, mlir::Attribute attr) {
10271027
llvm_unreachable("unexpected address-space attribute kind");
10281028
}
10291029

1030+
void printAddressSpaceValue(mlir::AsmPrinter &printer, cir::GlobalOp,
1031+
mlir::Attribute attr) {
1032+
printAddressSpaceValue(printer, attr);
1033+
}
1034+
10301035
mlir::Attribute cir::toCIRAddressSpaceAttr(mlir::MLIRContext *ctx,
10311036
clang::LangAS langAS) {
10321037
using clang::LangAS;

0 commit comments

Comments
 (0)