Skip to content

Commit 45c7bf4

Browse files
authoredAug 13, 2024··
[CIR][Dialect] Add verification of address space to get_global (#787)
This PR verifies `cir.get_global` has its result type correctly annotated with address space of the referenced symbol. The documentation is also updated to clarify this constraint. `GlobalOp` is the main consideration. It's worth noting that if the `cir.get_global` op references a function, we also (implicitly) checks that its result pointer type has no address space attribute.

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed
 

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,8 @@ def GetGlobalOp : CIR_Op<"get_global",
22312231
The `cir.get_global` operation retrieves the address pointing to a
22322232
named global variable. If the global variable is marked constant, writing
22332233
to the resulting address (such as through a `cir.store` operation) is
2234-
undefined. Resulting type must always be a `!cir.ptr<...>` type.
2234+
undefined. Resulting type must always be a `!cir.ptr<...>` type with the
2235+
same address space as the global variable.
22352236

22362237
Addresses of thread local globals can only be retrieved if this operation
22372238
is marked `thread_local`, which indicates the address isn't constant.
@@ -2241,6 +2242,9 @@ def GetGlobalOp : CIR_Op<"get_global",
22412242
%x = cir.get_global @foo : !cir.ptr<i32>
22422243
...
22432244
%y = cir.get_global thread_local @batata : !cir.ptr<i32>
2245+
...
2246+
cir.global external addrspace(offload_global) @gv = #cir.int<0> : !s32i
2247+
%z = cir.get_global @gv : !cir.ptr<!s32i, addrspace(offload_global)>
22442248
```
22452249
}];
22462250

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

+10
Original file line numberDiff line numberDiff line change
@@ -2044,8 +2044,10 @@ GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
20442044
<< "' does not reference a valid cir.global or cir.func";
20452045

20462046
mlir::Type symTy;
2047+
mlir::cir::AddressSpaceAttr symAddrSpace{};
20472048
if (auto g = dyn_cast<GlobalOp>(op)) {
20482049
symTy = g.getSymType();
2050+
symAddrSpace = g.getAddrSpaceAttr();
20492051
// Verify that for thread local global access, the global needs to
20502052
// be marked with tls bits.
20512053
if (getTls() && !g.getTlsModel())
@@ -2060,6 +2062,14 @@ GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
20602062
return emitOpError("result type pointee type '")
20612063
<< resultType.getPointee() << "' does not match type " << symTy
20622064
<< " of the global @" << getName();
2065+
2066+
if (symAddrSpace != resultType.getAddrSpace()) {
2067+
return emitOpError()
2068+
<< "result type address space does not match the address "
2069+
"space of the global @"
2070+
<< getName();
2071+
}
2072+
20632073
return success();
20642074
}
20652075

‎clang/test/CIR/IR/invalid.cir

+13
Original file line numberDiff line numberDiff line change
@@ -1272,3 +1272,16 @@ module {
12721272
}
12731273
}
12741274

1275+
// -----
1276+
1277+
!s32i = !cir.int<s, 32>
1278+
1279+
module {
1280+
cir.global external addrspace(offload_global) @gv = #cir.int<0> : !s32i
1281+
1282+
cir.func @test_get_global() {
1283+
// expected-error@+1 {{'cir.get_global' op result type address space does not match the address space of the global @gv}}
1284+
%addr = cir.get_global @gv : !cir.ptr<!s32i>
1285+
cir.return
1286+
}
1287+
}

0 commit comments

Comments
 (0)
Please sign in to comment.