Skip to content

Commit 49b0014

Browse files
gitoleggysit
andauthored
[mlir][llvm] adds an attribute for the module level assembly (#151318)
Adds support for the module level assembly in the LLVM IR dialect. --------- Co-authored-by: Tobias Gysi <[email protected]>
1 parent 7f5655c commit 49b0014

File tree

7 files changed

+62
-1
lines changed

7 files changed

+62
-1
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def LLVM_Dialect : Dialect {
8383
return "llvm.emit_c_interface";
8484
}
8585

86+
/// Name of the module level assembly attribute.
87+
static StringRef getModuleLevelAsmAttrName() { return "llvm.module_asm"; }
88+
8689
/// Name of the dependent libraries attribute.
8790
static StringRef getDependentLibrariesAttrName() {
8891
return "llvm.dependent_libraries";

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: 14 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,18 @@ void ModuleImport::convertTargetTriple() {
10631064
builder.getStringAttr(llvmModule->getTargetTriple().str()));
10641065
}
10651066

1067+
void ModuleImport::convertModuleLevelAsm() {
1068+
llvm::StringRef asmStr = llvmModule->getModuleInlineAsm();
1069+
llvm::SmallVector<mlir::Attribute> asmArrayAttr;
1070+
1071+
for (llvm::StringRef line : llvm::split(asmStr, '\n'))
1072+
if (!line.empty())
1073+
asmArrayAttr.push_back(builder.getStringAttr(line));
1074+
1075+
mlirModule->setAttr(LLVM::LLVMDialect::getModuleLevelAsmAttrName(),
1076+
builder.getArrayAttr(asmArrayAttr));
1077+
}
1078+
10661079
LogicalResult ModuleImport::convertFunctions() {
10671080
for (llvm::Function &func : llvmModule->functions())
10681081
if (failed(processFunction(&func)))
@@ -3195,5 +3208,6 @@ OwningOpRef<ModuleOp> mlir::translateLLVMIRToModule(
31953208
if (failed(moduleImport.convertIFuncs()))
31963209
return {};
31973210
moduleImport.convertTargetTriple();
3211+
moduleImport.convertModuleLevelAsm();
31983212
return module;
31993213
}

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,6 +2318,25 @@ prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
23182318
llvmModule->setTargetTriple(
23192319
llvm::Triple(cast<StringAttr>(targetTripleAttr).getValue()));
23202320

2321+
if (auto asmAttr = m->getDiscardableAttr(
2322+
LLVM::LLVMDialect::getModuleLevelAsmAttrName())) {
2323+
auto asmArrayAttr = dyn_cast<ArrayAttr>(asmAttr);
2324+
if (!asmArrayAttr) {
2325+
m->emitError("expected an array attribute for a module level asm");
2326+
return nullptr;
2327+
}
2328+
2329+
for (Attribute elt : asmArrayAttr) {
2330+
auto asmStrAttr = dyn_cast<StringAttr>(elt);
2331+
if (!asmStrAttr) {
2332+
m->emitError(
2333+
"expected a string attribute for each entry of a module level asm");
2334+
return nullptr;
2335+
}
2336+
llvmModule->appendModuleInlineAsm(asmStrAttr.getValue());
2337+
}
2338+
}
2339+
23212340
return llvmModule;
23222341
}
23232342

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"
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
// RUN: mlir-translate -verify-diagnostics -mlir-to-llvmir --no-implicit-module %s
1+
// RUN: mlir-translate -verify-diagnostics -mlir-to-llvmir --no-implicit-module -split-input-file %s
22

33
// expected-error@below {{'llvm.func' op can not be translated to an LLVMIR module}}
44
llvm.func @foo() {
55
llvm.return
66
}
7+
8+
// -----
9+
10+
// expected-error@below {{expected an array attribute for a module level asm}}
11+
module attributes {llvm.module_asm = "foo"} {}
12+
13+
// -----
14+
15+
// expected-error@below {{expected a string attribute for each entry of a module level asm}}
16+
module attributes {llvm.module_asm = [42]} {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
module attributes {llvm.module_asm = ["foo", "bar"]} {}
4+
5+
// CHECK: module asm "foo"
6+
// CHECK: module asm "bar"

0 commit comments

Comments
 (0)