[Mlir-commits] [mlir] a72e034 - [mlir] Add llvm.linker.options operation to the LLVM IR Dialect (#71720)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon Nov 13 06:13:09 PST 2023
Author: David Truby
Date: 2023-11-13T14:13:05Z
New Revision: a72e034f1308b0b8ec3ceca499d3913103b33b48
URL: https://github.com/llvm/llvm-project/commit/a72e034f1308b0b8ec3ceca499d3913103b33b48
DIFF: https://github.com/llvm/llvm-project/commit/a72e034f1308b0b8ec3ceca499d3913103b33b48.diff
LOG: [mlir] Add llvm.linker.options operation to the LLVM IR Dialect (#71720)
This patch adds a `llvm.linker.options` operation taking a list of
strings to pass to the linker when the resulting object file is linked.
This is particularly useful on Windows to specify the CRT version to use
for this object file.
Added:
mlir/test/Target/LLVMIR/Import/metadata-linker-options.ll
Modified:
mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
mlir/include/mlir/Target/LLVMIR/ModuleImport.h
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
mlir/lib/Target/LLVMIR/ModuleImport.cpp
mlir/test/Target/LLVMIR/llvmir-invalid.mlir
mlir/test/Target/LLVMIR/llvmir.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index ffb79a196db28ad..9fa6f23ce4de2dd 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -1797,4 +1797,33 @@ def LLVM_CallIntrinsicOp
let hasVerifier = 1;
}
+def LLVM_LinkerOptionsOp
+ : LLVM_Op<"linker_options"> {
+ let summary = "Options to pass to the linker when the object file is linked";
+ let description = [{
+ Pass the given options to the linker when the resulting object file is linked.
+ This is used extensively on Windows to determine the C runtime that the object
+ files should link against.
+
+ Examples:
+ ```mlir
+ // Link against the MSVC static threaded CRT.
+ llvm.linker_options ["/DEFAULTLIB:", "libcmt"]
+
+ // Link against aarch64 compiler-rt builtins
+ llvm.linker_options ["-l", "clang_rt.builtins-aarch64"]
+ ```
+ }];
+ let arguments = (ins StrArrayAttr:$options);
+ let assemblyFormat = [{
+ $options attr-dict
+ }];
+
+ let llvmBuilder = [{
+ convertLinkerOptionsOp($options, builder, moduleTranslation);
+ }];
+
+ let hasVerifier = 1;
+}
+
#endif // LLVMIR_OPS
diff --git a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
index 9bedc84e0bfa169..5f5adbff6c04ef8 100644
--- a/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
+++ b/mlir/include/mlir/Target/LLVMIR/ModuleImport.h
@@ -177,6 +177,10 @@ class ModuleImport {
/// implement the fastmath interface.
void setFastmathFlagsAttr(llvm::Instruction *inst, Operation *op) const;
+ /// Converts !llvm.linker.options metadata to the llvm.linker.options
+ /// LLVM dialect operation.
+ LogicalResult convertLinkerOptionsMetadata();
+
/// Converts all LLVM metadata nodes that translate to attributes such as
/// alias analysis or access group metadata, and builds a map from the
/// metadata nodes to the converted attributes.
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 6c8ea382b1dd855..f6c8f388732c3da 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -2912,6 +2912,17 @@ struct LLVMOpAsmDialectInterface : public OpAsmDialectInterface {
};
} // namespace
+//===----------------------------------------------------------------------===//
+// LinkerOptionsOp
+//===----------------------------------------------------------------------===//
+
+LogicalResult LinkerOptionsOp::verify() {
+ if (mlir::Operation *parentOp = (*this)->getParentOp();
+ parentOp && !satisfiesLLVMModule(parentOp))
+ return emitOpError("must appear at the module level");
+ return success();
+}
+
//===----------------------------------------------------------------------===//
// LLVMDialect initialization, type parsing, and registration.
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
index 5494a13acb6e1b6..83066f7d9fed315 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
@@ -172,6 +172,24 @@ convertCallLLVMIntrinsicOp(CallIntrinsicOp op, llvm::IRBuilderBase &builder,
return success();
}
+static void convertLinkerOptionsOp(ArrayAttr options,
+ llvm::IRBuilderBase &builder,
+ LLVM::ModuleTranslation &moduleTranslation) {
+ llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
+ llvm::LLVMContext &context = llvmModule->getContext();
+ llvm::NamedMDNode *linkerMDNode =
+ llvmModule->getOrInsertNamedMetadata("llvm.linker.options");
+ SmallVector<llvm::Metadata *> MDNodes;
+ MDNodes.reserve(options.size());
+ for (auto s : options.getAsRange<StringAttr>()) {
+ auto *MDNode = llvm::MDString::get(context, s.getValue());
+ MDNodes.push_back(MDNode);
+ }
+
+ auto *listMDNode = llvm::MDTuple::get(context, MDNodes);
+ linkerMDNode->addOperand(listMDNode);
+}
+
static LogicalResult
convertOperationImpl(Operation &opInst, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) {
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 1f13bdf44992ae5..dd2da66caf428bf 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -487,6 +487,23 @@ void ModuleImport::addDebugIntrinsic(llvm::CallInst *intrinsic) {
debugIntrinsics.insert(intrinsic);
}
+LogicalResult ModuleImport::convertLinkerOptionsMetadata() {
+ for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) {
+ if (named.getName() != "llvm.linker.options")
+ continue;
+ // llvm.linker.options operands are lists of strings.
+ for (const llvm::MDNode *md : named.operands()) {
+ SmallVector<StringRef> options;
+ options.reserve(md->getNumOperands());
+ for (const llvm::MDOperand &option : md->operands())
+ options.push_back(cast<llvm::MDString>(option)->getString());
+ builder.create<LLVM::LinkerOptionsOp>(mlirModule.getLoc(),
+ builder.getStrArrayAttr(options));
+ }
+ }
+ return success();
+}
+
LogicalResult ModuleImport::convertMetadata() {
OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToEnd(mlirModule.getBody());
@@ -513,6 +530,8 @@ LogicalResult ModuleImport::convertMetadata() {
return failure();
}
}
+ if (failed(convertLinkerOptionsMetadata()))
+ return failure();
return success();
}
diff --git a/mlir/test/Target/LLVMIR/Import/metadata-linker-options.ll b/mlir/test/Target/LLVMIR/Import/metadata-linker-options.ll
new file mode 100644
index 000000000000000..8702415c298862f
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/Import/metadata-linker-options.ll
@@ -0,0 +1,15 @@
+; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
+
+; CHECK: llvm.linker_options ["DEFAULTLIB:", "libcmt"]
+!llvm.linker.options = !{!0}
+!0 = !{!"DEFAULTLIB:", !"libcmt"}
+
+; // -----
+
+!llvm.linker.options = !{!0, !1, !2}
+; CHECK: llvm.linker_options ["DEFAULTLIB:", "libcmt"]
+!0 = !{!"DEFAULTLIB:", !"libcmt"}
+; CHECK: llvm.linker_options ["DEFAULTLIB:", "libcmtd"]
+!1 = !{!"DEFAULTLIB:", !"libcmtd"}
+; CHECK: llvm.linker_options ["-lm"]
+!2 = !{!"-lm"}
diff --git a/mlir/test/Target/LLVMIR/llvmir-invalid.mlir b/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
index 9b14f5814987d99..e531c3cb4e24093 100644
--- a/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir-invalid.mlir
@@ -253,3 +253,10 @@ llvm.comdat @__llvm_comdat_1 {
// expected-error @below{{comdat selection symbols must be unique even in
diff erent comdat regions}}
llvm.comdat_selector @foo any
}
+
+// -----
+
+llvm.func @foo() {
+ // expected-error @below{{must appear at the module level}}
+ llvm.linker_options ["test"]
+}
diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir
index 3f27b247edd3e67..73fcee0dd2e70bd 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -2324,3 +2324,9 @@ llvm.func @zeroinit_complex_local_aggregate() {
llvm.return
}
+
+//CHECK: !llvm.linker.options = !{![[MD0:[0-9]+]], ![[MD1:[0-9]+]]}
+//CHECK: ![[MD0]] = !{!"/DEFAULTLIB:", !"libcmt"}
+llvm.linker_options ["/DEFAULTLIB:", "libcmt"]
+//CHECK: ![[MD1]] = !{!"/DEFAULTLIB:", !"libcmtd"}
+llvm.linker_options ["/DEFAULTLIB:", "libcmtd"]
More information about the Mlir-commits
mailing list