diff --git a/mlir/lib/Target/LLVMIR/Dialect/NVVM/NVVMToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/NVVM/NVVMToLLVMIRTranslation.cpp index 18d42e9577095..254be8e63590f 100644 --- a/mlir/lib/Target/LLVMIR/Dialect/NVVM/NVVMToLLVMIRTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/Dialect/NVVM/NVVMToLLVMIRTranslation.cpp @@ -692,6 +692,13 @@ class NVVMDialectLLVMIRTranslationInterface LogicalResult convertOperation(Operation *op, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation) const final { + // All NVVM ops are instruction-level and require an active insertion point. + // A null insert block means the op is misplaced (e.g., at module scope), + // which would otherwise cause a null dereference in createIntrinsicCall. + if (!builder.GetInsertBlock()) + return op->emitOpError( + "cannot be translated to LLVM IR without an active insertion " + "point; make sure the op is inside a function"); Operation &opInst = *op; #include "mlir/Dialect/LLVMIR/NVVMConversions.inc" diff --git a/mlir/test/Target/LLVMIR/nvvmir-invalid.mlir b/mlir/test/Target/LLVMIR/nvvmir-invalid.mlir index c0fe0fa11f497..785f59b22801f 100644 --- a/mlir/test/Target/LLVMIR/nvvmir-invalid.mlir +++ b/mlir/test/Target/LLVMIR/nvvmir-invalid.mlir @@ -595,7 +595,7 @@ func.func @invalid_range_equal_bounds() { // ----- -// Test for correct return type check for wmma.load fragment a for f64 +// Test for correct return type check for wmma.load fragment a for f64 llvm.func @nvvm_wmma_load_a_f64(%arg0: !llvm.ptr, %arg1 : i32) { // expected-error @below {{'nvvm.wmma.load' op expected destination type to be f64}} %0 = nvvm.wmma.load %arg0, %arg1 @@ -603,3 +603,12 @@ llvm.func @nvvm_wmma_load_a_f64(%arg0: !llvm.ptr, %arg1 : i32) { : (!llvm.ptr) -> !llvm.struct<(f64)> llvm.return } + +// ----- + +// Test that nvvm.barrier0 at module scope (outside any function) produces a +// proper error instead of crashing with a null dereference in +// createIntrinsicCall. See https://github.com/llvm/llvm-project/issues/186642 +// expected-error @+2 {{'nvvm.barrier0' op cannot be translated to LLVM IR without an active insertion point}} +// expected-error @+1 {{LLVM Translation failed for operation: nvvm.barrier0}} +nvvm.barrier0