Unverified Commit a72e034f authored by David Truby's avatar David Truby Committed by GitHub
Browse files

[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.
parent 002da67d
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -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
+4 −0
Original line number Diff line number Diff line
@@ -177,6 +177,10 @@ public:
  /// 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.
+11 −0
Original line number Diff line number Diff line
@@ -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.
//===----------------------------------------------------------------------===//
+18 −0
Original line number Diff line number Diff line
@@ -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) {
+19 −0
Original line number Diff line number Diff line
@@ -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();
}

Loading