Skip to content

[CIR] Reject a global carrying a function type - #214065

Merged
adams381 merged 1 commit into
llvm:mainfrom
adams381:users/adams381/cir-global-reject-func-type
Aug 4, 2026
Merged

[CIR] Reject a global carrying a function type#214065
adams381 merged 1 commit into
llvm:mainfrom
adams381:users/adams381/cir-global-reject-func-type

Conversation

@adams381

@adams381 adams381 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

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.

Assisted-by: Cursor / claude-opus-5

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.
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Aug 4, 2026
@llvmorg-github-actions

llvmorg-github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

@llvm/pr-subscribers-clangir

@llvm/pr-subscribers-clang

Author: Adam Smith (adams381)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/214065.diff

2 Files Affected:

  • (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+5)
  • (modified) clang/test/CIR/IR/invalid-global.cir (+9)
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<()>
+
+}

@adams381
adams381 requested review from erichkeane and lanza August 4, 2026 20:39
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()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you get here with C/C++? How do we lower this? What was hte original source that got us here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@adams381
adams381 merged commit bc6f2a9 into llvm:main Aug 4, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants