[Mlir-commits] [mlir] [mlir][LLVMIR] Require active insertion point for llvm.call translation (PR #222920)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Sep 11 04:06:28 PDT 2026
https://github.com/AkunCube updated https://github.com/llvm/llvm-project/pull/222920
>From a2eb5ba4891884f06e233195a2e4c804306f73e3 Mon Sep 17 00:00:00 2001
From: cubehan3 <cubehan3 at gmail.com>
Date: Fri, 11 Sep 2026 11:03:04 +0000
Subject: [PATCH] [mlir][LLVMIR] Require active insertion point for llvm.call
translation
---
.../LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp | 8 ++++++++
mlir/test/Target/LLVMIR/llvmir-invalid.mlir | 10 ++++++++++
2 files changed, 18 insertions(+)
diff --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
index 0c715da6ba954..1bb08e8bf6fed 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
@@ -442,6 +442,14 @@ convertOperationImpl(Operation &opInst, llvm::IRBuilderBase &builder,
// itself. Otherwise, this is an indirect call and the callee is the first
// operand, look it up as a normal value.
if (auto callOp = dyn_cast<LLVM::CallOp>(opInst)) {
+ // A null insert block means the op is misplaced (e.g., at module scope),
+ // which would otherwise leave a parentless CallInst that crashes the
+ // llvm::Module destructor ("Uses remain when a value is destroyed").
+ if (!builder.GetInsertBlock())
+ return opInst.emitOpError(
+ "cannot be translated to LLVM IR without an active insertion "
+ "point; make sure the op is inside a function");
+
auto operands = moduleTranslation.lookupValues(callOp.getCalleeOperands());
SmallVector<llvm::OperandBundleDef> opBundles =
convertOperandBundles(callOp.getOpBundleOperands(),
diff --git a/mlir/test/Target/LLVMIR/llvmir-invalid.mlir b/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
index 6e8301bb626e3..dfef2bf845ebc 100644
--- a/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
@@ -429,3 +429,13 @@ llvm.func @invoke_branch_weights() -> i32 attributes {personality = @__gxx_perso
^bb2: // 2 preds: ^bb0, ^bb1
llvm.return %0 : i32
}
+
+// -----
+
+// Test that llvm.call at module scope (outside any function) produces a proper
+// error instead of leaving a parentless CallInst that crashes the module
+// destructor during LLVM IR translation.
+llvm.func @barrier()
+// expected-error @+2 {{'llvm.call' op cannot be translated to LLVM IR without an active insertion point}}
+// expected-error @+1 {{LLVM Translation failed for operation: llvm.call}}
+llvm.call @barrier() : () -> ()
More information about the Mlir-commits
mailing list