Skip to content

[CIR][Dialect] Add verification of address space to get_global #787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,8 @@ def GetGlobalOp : CIR_Op<"get_global",
The `cir.get_global` operation retrieves the address pointing to a
named global variable. If the global variable is marked constant, writing
to the resulting address (such as through a `cir.store` operation) is
undefined. Resulting type must always be a `!cir.ptr<...>` type.
undefined. Resulting type must always be a `!cir.ptr<...>` type with the
same address space as the global variable.

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

Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2044,8 +2044,10 @@ GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
<< "' does not reference a valid cir.global or cir.func";

mlir::Type symTy;
mlir::cir::AddressSpaceAttr symAddrSpace{};
if (auto g = dyn_cast<GlobalOp>(op)) {
symTy = g.getSymType();
symAddrSpace = g.getAddrSpaceAttr();
// Verify that for thread local global access, the global needs to
// be marked with tls bits.
if (getTls() && !g.getTlsModel())
Expand All @@ -2060,6 +2062,14 @@ GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
return emitOpError("result type pointee type '")
<< resultType.getPointee() << "' does not match type " << symTy
<< " of the global @" << getName();

if (symAddrSpace != resultType.getAddrSpace()) {
return emitOpError()
<< "result type address space does not match the address "
"space of the global @"
<< getName();
}

return success();
}

Expand Down
13 changes: 13 additions & 0 deletions clang/test/CIR/IR/invalid.cir
Original file line number Diff line number Diff line change
Expand Up @@ -1272,3 +1272,16 @@ module {
}
}

// -----

!s32i = !cir.int<s, 32>

module {
cir.global external addrspace(offload_global) @gv = #cir.int<0> : !s32i

cir.func @test_get_global() {
// expected-error@+1 {{'cir.get_global' op result type address space does not match the address space of the global @gv}}
%addr = cir.get_global @gv : !cir.ptr<!s32i>
cir.return
}
}