Skip to content

Commit 7a20b31

Browse files
committed
added import implementation
1 parent 5bdb44d commit 7a20b31

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

mlir/include/mlir/Target/LLVMIR/ModuleImport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ class ModuleImport {
8383
/// specification.
8484
void convertTargetTriple();
8585

86+
/// Converts the module level asm of the LLVM module to an MLIR module
87+
/// level asm specification.
88+
void convertModuleLevelAsm();
89+
8690
/// Stores the mapping between an LLVM value and its MLIR counterpart.
8791
void mapValue(llvm::Value *llvm, Value mlir) { mapValue(llvm) = mlir; }
8892

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "llvm/ADT/DepthFirstIterator.h"
3131
#include "llvm/ADT/PostOrderIterator.h"
3232
#include "llvm/ADT/ScopeExit.h"
33+
#include "llvm/ADT/StringExtras.h"
3334
#include "llvm/ADT/TypeSwitch.h"
3435
#include "llvm/IR/Comdat.h"
3536
#include "llvm/IR/Constants.h"
@@ -1063,6 +1064,19 @@ void ModuleImport::convertTargetTriple() {
10631064
builder.getStringAttr(llvmModule->getTargetTriple().str()));
10641065
}
10651066

1067+
void ModuleImport::convertModuleLevelAsm() {
1068+
auto str = llvmModule->getModuleInlineAsm();
1069+
llvm::SmallVector<mlir::Attribute> arr;
1070+
1071+
for (auto line : llvm::split(str, '\n'))
1072+
if (!line.empty())
1073+
arr.push_back(builder.getStringAttr(line));
1074+
1075+
mlirModule->setAttr(
1076+
LLVM::LLVMDialect::getModuleLevelAsmAttrName(),
1077+
builder.getArrayAttr(arr));
1078+
}
1079+
10661080
LogicalResult ModuleImport::convertFunctions() {
10671081
for (llvm::Function &func : llvmModule->functions())
10681082
if (failed(processFunction(&func)))
@@ -3199,5 +3213,6 @@ OwningOpRef<ModuleOp> mlir::translateLLVMIRToModule(
31993213
if (failed(moduleImport.convertIFuncs()))
32003214
return {};
32013215
moduleImport.convertTargetTriple();
3216+
moduleImport.convertModuleLevelAsm();
32023217
return module;
32033218
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; RUN: mlir-translate -import-llvm %s | FileCheck %s
2+
; CHECK: llvm.module_asm = ["foo", "bar"]
3+
4+
module asm "foo"
5+
module asm "bar"

0 commit comments

Comments
 (0)