[CIR] Reject a global carrying a function type - #214065
Conversation
The verifier was accepting a `cir.global` whose `sym_type` is a function. The LLVM dialect global takes one too, so it survives to LLVM IR translation and crashes instead of reporting an error. `GlobalOp::verify()` now rejects it.
|
@llvm/pr-subscribers-clangir @llvm/pr-subscribers-clang Author: Adam Smith (adams381) ChangesThe verifier was accepting a Full diff: https://github.com/llvm/llvm-project/pull/214065.diff 2 Files Affected:
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index f8c34552eca11..cd94219655e02 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -2076,6 +2076,11 @@ static void printConstant(OpAsmPrinter &p, Attribute value) {
}
mlir::LogicalResult cir::GlobalOp::verify() {
+ // A function is not an object, so it cannot be the type of a global. A
+ // global that holds a function's address carries a pointer type instead.
+ if (mlir::isa<cir::FuncType>(getSymType()))
+ return emitOpError("global type cannot be a function type");
+
// Verify that the initial value, if present, is either a unit attribute or
// an attribute CIR supports.
if (getInitialValue().has_value()) {
diff --git a/clang/test/CIR/IR/invalid-global.cir b/clang/test/CIR/IR/invalid-global.cir
index d0a2f0d116c7f..12acc2e6333c8 100644
--- a/clang/test/CIR/IR/invalid-global.cir
+++ b/clang/test/CIR/IR/invalid-global.cir
@@ -40,3 +40,12 @@ cir.global external @bad_alias_with_dtor alias(@target) = #cir.int<7> : !s32i dt
}
}
+
+// -----
+
+module {
+
+// expected-error @below {{global type cannot be a function type}}
+cir.global "private" external @bad_func_type : !cir.func<()>
+
+}
|
| mlir::LogicalResult cir::GlobalOp::verify() { | ||
| // A function is not an object, so it cannot be the type of a global. A | ||
| // global that holds a function's address carries a pointer type instead. | ||
| if (mlir::isa<cir::FuncType>(getSymType())) |
There was a problem hiding this comment.
Can you get here with C/C++? How do we lower this? What was hte original source that got us here?
There was a problem hiding this comment.
You cannot get here with C/C++. This was discovered when hand writing CIR for function pointer calling convention lowering support (that PR is going up soon).
It lowers to an llvm.mlir.global carrying a function type. The LLVM dialect verifier accepts it and then LLVM IR translation aborts in the GlobalVariable constructor with no error message.
The verifier was accepting a
cir.globalwhosesym_typeis a function. The LLVM dialect global takes one too, so it survives to LLVM IR translation and crashes instead of reporting an error.GlobalOp::verify()now rejects it.Assisted-by: Cursor / claude-opus-5