[Mlir-commits] [flang] [mlir] [MLIR][LLVMIR][DLTI] Add #llvm.target, #llvm.data_layout and TargetAttrInterface (PR #145899)

Tobias Gysi llvmlistbot at llvm.org
Sat Jul 26 11:49:57 PDT 2025


================
@@ -0,0 +1,61 @@
+//===- DataLayoutFromTarget.cpp - extract data layout from TargetMachine --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/DLTI/DLTI.h"
+#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/Pass/Pass.h"
+
+namespace mlir {
+namespace LLVM {
+#define GEN_PASS_DEF_LLVMDATALAYOUTFROMTARGET
+#include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc"
+} // namespace LLVM
+} // namespace mlir
+
+using namespace mlir;
+
+struct DataLayoutFromTargetPass
+    : public LLVM::impl::LLVMDataLayoutFromTargetBase<
+          DataLayoutFromTargetPass> {
+  void runOnOperation() override {
+    ModuleOp mod = getOperation();
+
+    bool passFailed = false;
+
+    mod->walk([&](ModuleOp mod) {
+      auto targetAttr =
+          mod->getAttrOfType<LLVM::TargetAttrInterface>("llvm.target");
+      if (!targetAttr)
+        return;
+
+      FailureOr<llvm::DataLayout> dataLayout = targetAttr.getDataLayout();
+      if (failed(dataLayout)) {
+        mod->emitError() << "failed to obtain llvm::DataLayout from "
+                         << targetAttr;
+        passFailed = true;
+        return;
+      }
+      auto dataLayoutAttr = LLVM::DataLayoutAttr::get(
+          &getContext(), dataLayout->getStringRepresentation());
+
+      StringRef dlSpecIdentifier = "dlti.dl_spec";
----------------
gysit wrote:

I believe you could use `DLTIDialect::kDataLayoutAttrName` here?

https://github.com/llvm/llvm-project/pull/145899


More information about the Mlir-commits mailing list