-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[mlir] Add convertInstruction
and getSupportedInstructions
to LLVMImportInterface
#86799
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
; RUN: mlir-translate -test-import-llvmir %s | FileCheck %s | ||
|
||
; CHECK-LABEL: @custom_load | ||
; CHECK-SAME: %[[PTR:[[:alnum:]]+]] | ||
define double @custom_load(ptr %ptr) { | ||
; CHECK: %[[LOAD:[0-9]+]] = llvm.load %[[PTR]] : !llvm.ptr -> f64 | ||
; CHECK: %[[TEST:[0-9]+]] = "test.same_operand_element_type"(%[[LOAD]], %[[LOAD]]) : (f64, f64) -> f64 | ||
%1 = load double, ptr %ptr | ||
; CHECK: llvm.return %[[TEST]] : f64 | ||
ret double %1 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
mlir/test/lib/Dialect/Test/TestFromLLVMIRTranslation.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
//===- TestFromLLVMIRTranslation.cpp - Import Test dialect from LLVM IR ---===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements a translation between LLVM IR and the MLIR Test dialect. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "TestDialect.h" | ||
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/IR/BuiltinOps.h" | ||
#include "mlir/Support/LLVM.h" | ||
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.h" | ||
#include "mlir/Target/LLVMIR/Import.h" | ||
#include "mlir/Target/LLVMIR/ModuleImport.h" | ||
#include "mlir/Tools/mlir-translate/Translation.h" | ||
|
||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/Verifier.h" | ||
#include "llvm/IRReader/IRReader.h" | ||
#include "llvm/Support/SourceMgr.h" | ||
|
||
using namespace mlir; | ||
using namespace test; | ||
|
||
static ArrayRef<unsigned> getSupportedInstructionsImpl() { | ||
static unsigned instructions[] = {llvm::Instruction::Load}; | ||
return instructions; | ||
} | ||
|
||
static LogicalResult convertLoad(OpBuilder &builder, llvm::Instruction *inst, | ||
ArrayRef<llvm::Value *> llvmOperands, | ||
LLVM::ModuleImport &moduleImport) { | ||
FailureOr<Value> addr = moduleImport.convertValue(llvmOperands[0]); | ||
if (failed(addr)) | ||
return failure(); | ||
// Create the LoadOp | ||
Value loadOp = builder.create<LLVM::LoadOp>( | ||
moduleImport.translateLoc(inst->getDebugLoc()), | ||
moduleImport.convertType(inst->getType()), *addr); | ||
moduleImport.mapValue(inst) = builder.create<SameOperandElementTypeOp>( | ||
loadOp.getLoc(), loadOp.getType(), loadOp, loadOp); | ||
return success(); | ||
} | ||
|
||
namespace { | ||
class TestDialectLLVMImportDialectInterface | ||
: public LLVMImportDialectInterface { | ||
public: | ||
using LLVMImportDialectInterface::LLVMImportDialectInterface; | ||
|
||
LogicalResult | ||
convertInstruction(OpBuilder &builder, llvm::Instruction *inst, | ||
ArrayRef<llvm::Value *> llvmOperands, | ||
LLVM::ModuleImport &moduleImport) const override { | ||
switch (inst->getOpcode()) { | ||
case llvm::Instruction::Load: | ||
return convertLoad(builder, inst, llvmOperands, moduleImport); | ||
default: | ||
break; | ||
} | ||
return failure(); | ||
} | ||
|
||
ArrayRef<unsigned> getSupportedInstructions() const override { | ||
return getSupportedInstructionsImpl(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
namespace mlir { | ||
void registerTestFromLLVMIR() { | ||
TranslateToMLIRRegistration registration( | ||
"test-import-llvmir", "test dialect from LLVM IR", | ||
[](llvm::SourceMgr &sourceMgr, | ||
MLIRContext *context) -> OwningOpRef<Operation *> { | ||
llvm::SMDiagnostic err; | ||
llvm::LLVMContext llvmContext; | ||
std::unique_ptr<llvm::Module> llvmModule = | ||
llvm::parseIR(*sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), | ||
err, llvmContext); | ||
if (!llvmModule) { | ||
std::string errStr; | ||
llvm::raw_string_ostream errStream(errStr); | ||
err.print(/*ProgName=*/"", errStream); | ||
emitError(UnknownLoc::get(context)) << errStream.str(); | ||
return {}; | ||
} | ||
if (llvm::verifyModule(*llvmModule, &llvm::errs())) | ||
return nullptr; | ||
|
||
return translateLLVMIRToModule(std::move(llvmModule), context, false); | ||
}, | ||
[](DialectRegistry ®istry) { | ||
registry.insert<DLTIDialect>(); | ||
registry.insert<test::TestDialect>(); | ||
registerLLVMDialectImport(registry); | ||
registry.addExtension( | ||
+[](MLIRContext *ctx, test::TestDialect *dialect) { | ||
dialect->addInterfaces<TestDialectLLVMImportDialectInterface>(); | ||
}); | ||
}); | ||
} | ||
} // namespace mlir |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if multiple dialect return overlapping results here?
Also the documentation is saying what this does, but not why it is useful or how this should be used, can you expand?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If multiple dialects try to register the same instruction the interface emits an error and aborts the entire translation, see the
initializeImport
method.Small caveat: this mechanism doesn't check if it's colliding with LLVM tablegen conversions, only with respect to other interfaces -which I'd argue is okay, bc allows overriding LLVM behavior.
With respect to how should be used, I added a test where:
gets imported as:
The example shows how to use the mechanism.
I'll add to the docs that this allows overriding LLVM behavior.