Skip to content
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

[mlir] Add llvm.linker.options operation to the LLVM IR Dialect #71720

Merged
merged 5 commits into from
Nov 13, 2023
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
29 changes: 29 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1825,4 +1825,33 @@ def LLVM_CallIntrinsicOp
let hasVerifier = 1;
}

def LLVM_LinkerOptionsOp
Dinistro marked this conversation as resolved.
Show resolved Hide resolved
: 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
4 changes: 4 additions & 0 deletions mlir/include/mlir/Target/LLVMIR/ModuleImport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,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.
//===----------------------------------------------------------------------===//
Expand Down
18 changes: 18 additions & 0 deletions mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
joker-eph marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down
19 changes: 19 additions & 0 deletions mlir/lib/Target/LLVMIR/ModuleImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
joker-eph marked this conversation as resolved.
Show resolved Hide resolved
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());
Expand All @@ -513,6 +530,8 @@ LogicalResult ModuleImport::convertMetadata() {
return failure();
}
}
if (failed(convertLinkerOptionsMetadata()))
return failure();
return success();
}

Expand Down
15 changes: 15 additions & 0 deletions mlir/test/Target/LLVMIR/Import/metadata-linker-options.ll
Original file line number Diff line number Diff line change
@@ -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"}
7 changes: 7 additions & 0 deletions mlir/test/Target/LLVMIR/llvmir-invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,10 @@ llvm.comdat @__llvm_comdat_1 {
// expected-error @below{{comdat selection symbols must be unique even in different comdat regions}}
llvm.comdat_selector @foo any
}

// -----

llvm.func @foo() {
// expected-error @below{{must appear at the module level}}
llvm.linker_options ["test"]
}
6 changes: 6 additions & 0 deletions mlir/test/Target/LLVMIR/llvmir.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2337,3 +2337,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"]
Loading