Skip to content

[MLIR][LLVMIR] Import: add flag to prefer using unregistered intrinsics #130685

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 3 commits into from
Mar 15, 2025
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
14 changes: 9 additions & 5 deletions mlir/include/mlir/Target/LLVMIR/Import.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ class ModuleOp;
/// adversarial inputs.
/// The `loadAllDialects` flag (default on) will load all dialects in the
/// context.
OwningOpRef<ModuleOp>
translateLLVMIRToModule(std::unique_ptr<llvm::Module> llvmModule,
MLIRContext *context, bool emitExpensiveWarnings = true,
bool dropDICompositeTypeElements = false,
bool loadAllDialects = true);
/// The `preferUnregisteredIntrinsics` flag (default off) controls whether to
/// import all intrinsics using `llvm.intrinsic_call` even if a dialect
/// registered an explicit intrinsic operation. Warning: passes that rely on
/// matching explicit intrinsic operations may not work properly if this flag is
/// enabled.
OwningOpRef<ModuleOp> translateLLVMIRToModule(
std::unique_ptr<llvm::Module> llvmModule, MLIRContext *context,
bool emitExpensiveWarnings = true, bool dropDICompositeTypeElements = false,
bool loadAllDialects = true, bool preferUnregisteredIntrinsics = false);

/// Translate the given LLVM data layout into an MLIR equivalent using the DLTI
/// dialect.
Expand Down
21 changes: 1 addition & 20 deletions mlir/include/mlir/Target/LLVMIR/LLVMImportInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,7 @@ class LLVMImportInterface
/// Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
/// Returns failure otherwise.
LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst,
LLVM::ModuleImport &moduleImport) const {
// Lookup the dialect interface for the given intrinsic.
// Verify the intrinsic identifier maps to an actual intrinsic.
llvm::Intrinsic::ID intrinId = inst->getIntrinsicID();
assert(intrinId != llvm::Intrinsic::not_intrinsic);

// First lookup the intrinsic across different dialects for known
// supported conversions, examples include arm-neon, nvm-sve, etc.
Dialect *dialect = intrinsicToDialect.lookup(intrinId);

// No specialized (supported) intrinsics, attempt to generate a generic
// version via llvm.call_intrinsic (if available).
if (!dialect)
return convertUnregisteredIntrinsic(builder, inst, moduleImport);

// Dispatch the conversion to the dialect interface.
const LLVMImportDialectInterface *iface = getInterfaceFor(dialect);
assert(iface && "expected to find a dialect interface");
return iface->convertIntrinsic(builder, inst, moduleImport);
}
LLVM::ModuleImport &moduleImport) const;

/// Returns true if the given LLVM IR intrinsic is convertible to an MLIR
/// operation.
Expand Down
13 changes: 12 additions & 1 deletion mlir/include/mlir/Target/LLVMIR/ModuleImport.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class LoopAnnotationImporter;
class ModuleImport {
public:
ModuleImport(ModuleOp mlirModule, std::unique_ptr<llvm::Module> llvmModule,
bool emitExpensiveWarnings, bool importEmptyDICompositeTypes);
bool emitExpensiveWarnings, bool importEmptyDICompositeTypes,
bool preferUnregisteredIntrinsics);

/// Calls the LLVMImportInterface initialization that queries the registered
/// dialect interfaces for the supported LLVM IR intrinsics and metadata kinds
Expand Down Expand Up @@ -284,6 +285,12 @@ class ModuleImport {
void convertParameterAttributes(llvm::CallBase *call, ArrayAttr &argsAttr,
ArrayAttr &resAttr, OpBuilder &builder);

/// Whether the importer should try to convert all intrinsics to
/// llvm.call_intrinsic instead of dialect supported operations.
bool useUnregisteredIntrinsicsOnly() const {
return preferUnregisteredIntrinsics;
}

private:
/// Clears the accumulated state before processing a new region.
void clearRegionState() {
Expand Down Expand Up @@ -481,6 +488,10 @@ class ModuleImport {
/// emitted. Avoids generating warnings for unhandled debug intrinsics and
/// metadata that otherwise dominate the translation time for large inputs.
bool emitExpensiveWarnings;

/// An option to control whether the importer should try to convert all
/// intrinsics to llvm.call_intrinsic instead of dialect supported operations.
bool preferUnregisteredIntrinsics;
};

} // namespace LLVM
Expand Down
14 changes: 11 additions & 3 deletions mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ void registerFromLLVMIRTranslation() {
"the LLVM IR import (discouraged: testing only!)"),
llvm::cl::init(false));

static llvm::cl::opt<bool> preferUnregisteredIntrinsics(
"prefer-unregistered-intrinsics",
llvm::cl::desc(
"Prefer translating all intrinsics into llvm.call_intrinsic instead "
"of using dialect supported intrinsics"),
llvm::cl::init(false));

TranslateToMLIRRegistration registration(
"import-llvm", "Translate LLVMIR to MLIR",
[](llvm::SourceMgr &sourceMgr,
Expand All @@ -60,9 +67,10 @@ void registerFromLLVMIRTranslation() {
if (llvmModule->IsNewDbgInfoFormat)
llvmModule->convertFromNewDbgValues();

return translateLLVMIRToModule(std::move(llvmModule), context,
emitExpensiveWarnings,
dropDICompositeTypeElements);
return translateLLVMIRToModule(
std::move(llvmModule), context, emitExpensiveWarnings,
dropDICompositeTypeElements, /*loadAllDialects=*/true,
preferUnregisteredIntrinsics);
},
[](DialectRegistry &registry) {
// Register the DLTI dialect used to express the data layout
Expand Down
28 changes: 28 additions & 0 deletions mlir/lib/Target/LLVMIR/LLVMImportInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,31 @@ LogicalResult mlir::LLVMImportInterface::convertUnregisteredIntrinsic(

return success();
}

/// Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
/// Returns failure otherwise.
LogicalResult mlir::LLVMImportInterface::convertIntrinsic(
OpBuilder &builder, llvm::CallInst *inst,
LLVM::ModuleImport &moduleImport) const {
// Lookup the dialect interface for the given intrinsic.
// Verify the intrinsic identifier maps to an actual intrinsic.
llvm::Intrinsic::ID intrinId = inst->getIntrinsicID();
assert(intrinId != llvm::Intrinsic::not_intrinsic);

// First lookup the intrinsic across different dialects for known
// supported conversions, examples include arm-neon, nvm-sve, etc.
Dialect *dialect = nullptr;

if (!moduleImport.useUnregisteredIntrinsicsOnly())
dialect = intrinsicToDialect.lookup(intrinId);

// No specialized (supported) intrinsics, attempt to generate a generic
// version via llvm.call_intrinsic (if available).
if (!dialect)
return convertUnregisteredIntrinsic(builder, inst, moduleImport);

// Dispatch the conversion to the dialect interface.
const LLVMImportDialectInterface *iface = getInterfaceFor(dialect);
assert(iface && "expected to find a dialect interface");
return iface->convertIntrinsic(builder, inst, moduleImport);
}
18 changes: 10 additions & 8 deletions mlir/lib/Target/LLVMIR/ModuleImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ getTopologicallySortedBlocks(ArrayRef<llvm::BasicBlock *> basicBlocks) {
ModuleImport::ModuleImport(ModuleOp mlirModule,
std::unique_ptr<llvm::Module> llvmModule,
bool emitExpensiveWarnings,
bool importEmptyDICompositeTypes)
bool importEmptyDICompositeTypes,
bool preferUnregisteredIntrinsics)
: builder(mlirModule->getContext()), context(mlirModule->getContext()),
mlirModule(mlirModule), llvmModule(std::move(llvmModule)),
iface(mlirModule->getContext()),
Expand All @@ -171,7 +172,8 @@ ModuleImport::ModuleImport(ModuleOp mlirModule,
mlirModule, importEmptyDICompositeTypes)),
loopAnnotationImporter(
std::make_unique<LoopAnnotationImporter>(*this, builder)),
emitExpensiveWarnings(emitExpensiveWarnings) {
emitExpensiveWarnings(emitExpensiveWarnings),
preferUnregisteredIntrinsics(preferUnregisteredIntrinsics) {
builder.setInsertionPointToStart(mlirModule.getBody());
}

Expand Down Expand Up @@ -2552,11 +2554,10 @@ ModuleImport::translateDereferenceableAttr(const llvm::MDNode *node,
return derefAttr;
}

OwningOpRef<ModuleOp>
mlir::translateLLVMIRToModule(std::unique_ptr<llvm::Module> llvmModule,
MLIRContext *context, bool emitExpensiveWarnings,
bool dropDICompositeTypeElements,
bool loadAllDialects) {
OwningOpRef<ModuleOp> mlir::translateLLVMIRToModule(
std::unique_ptr<llvm::Module> llvmModule, MLIRContext *context,
bool emitExpensiveWarnings, bool dropDICompositeTypeElements,
bool loadAllDialects, bool preferUnregisteredIntrinsics) {
// Preload all registered dialects to allow the import to iterate the
// registered LLVMImportDialectInterface implementations and query the
// supported LLVM IR constructs before starting the translation. Assumes the
Expand All @@ -2573,7 +2574,8 @@ mlir::translateLLVMIRToModule(std::unique_ptr<llvm::Module> llvmModule,
/*column=*/0)));

ModuleImport moduleImport(module.get(), std::move(llvmModule),
emitExpensiveWarnings, dropDICompositeTypeElements);
emitExpensiveWarnings, dropDICompositeTypeElements,
preferUnregisteredIntrinsics);
if (failed(moduleImport.initializeImportInterface()))
return {};
if (failed(moduleImport.convertDataLayout()))
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Target/LLVMIR/Import/intrinsic-prefer-unregistered.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
; RUN: mlir-translate -import-llvm -prefer-unregistered-intrinsics %s | FileCheck %s

; CHECK-LABEL: llvm.func @lifetime
define void @lifetime(ptr %0) {
; CHECK: llvm.call_intrinsic "llvm.lifetime.start.p0"({{.*}}, %arg0) : (i64, !llvm.ptr {llvm.nonnull}) -> !llvm.void
call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %0)
; CHECK: llvm.call_intrinsic "llvm.lifetime.end.p0"({{.*}}, %arg0) : (i64, !llvm.ptr {llvm.nonnull}) -> !llvm.void
call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %0)
ret void
}