https://github.com/aokblast created 
https://github.com/llvm/llvm-project/pull/244797

MLIR spell data layout in their own ways. It causes two compatibly layout 
string not the same order become mismatch. Add a canonicalize function to 
tranlate to llvm one if two are the same layout.

>From 260ed9faa852186ac065d3d5879be9902d055eff Mon Sep 17 00:00:00 2001
From: ShengYi Hung <[email protected]>
Date: Fri, 20 Nov 2026 20:47:15 -0600
Subject: [PATCH] [CIR] Canonicalize data layoutin to llvm tranlation

MLIR spell data layout in their own ways. It causes two compatibly
layout string not the same order become mismatch. Add a canonicalize
function to tranlate to llvm one if two are the same layout.
---
 .../cir-translate-data-layout-compatible.cir  | 15 +++++++
 .../cir-translate-data-layout-from-cirgen.c   | 13 ++++++
 clang/tools/cir-translate/cir-translate.cpp   | 42 +++++++++++++++----
 3 files changed, 61 insertions(+), 9 deletions(-)
 create mode 100644 
clang/test/CIR/Tools/cir-translate-data-layout-compatible.cir
 create mode 100644 clang/test/CIR/Tools/cir-translate-data-layout-from-cirgen.c

diff --git a/clang/test/CIR/Tools/cir-translate-data-layout-compatible.cir 
b/clang/test/CIR/Tools/cir-translate-data-layout-compatible.cir
new file mode 100644
index 00000000000000..bccac589455aed
--- /dev/null
+++ b/clang/test/CIR/Tools/cir-translate-data-layout-compatible.cir
@@ -0,0 +1,15 @@
+// REQUIRES: x86-registered-target
+// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu 
--disable-cc-lowering %s -o %t.ll
+// RUN: FileCheck %s -input-file %t.ll
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-obj -x ir %t.ll -o 
/dev/null
+
+// The data layout cir-translate derives from the triple comes out spelled the
+// way the target spells it, which is the only one clang accepts.
+
+module {
+  cir.func @foo() {
+    cir.return
+  }
+}
+
+// CHECK: target datalayout = 
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
diff --git a/clang/test/CIR/Tools/cir-translate-data-layout-from-cirgen.c 
b/clang/test/CIR/Tools/cir-translate-data-layout-from-cirgen.c
new file mode 100644
index 00000000000000..72dd2c0babd7a7
--- /dev/null
+++ b/clang/test/CIR/Tools/cir-translate-data-layout-from-cirgen.c
@@ -0,0 +1,13 @@
+// REQUIRES: x86-registered-target
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o 
%t.cir
+// RUN: cir-translate --cir-to-llvmir --disable-cc-lowering %t.cir -o %t.ll
+// RUN: FileCheck %s -input-file %t.ll
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-obj -x ir %t.ll -o 
/dev/null
+
+// The data layout CIRGen records in the module is the target's, so it comes
+// back out of cir-translate as the target's: no i1 entry, which
+// llvm::DataLayout does not have, and the target's spelling.
+
+int foo(_Bool b) { return b; }
+
+// CHECK: target datalayout = 
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
diff --git a/clang/tools/cir-translate/cir-translate.cpp 
b/clang/tools/cir-translate/cir-translate.cpp
index c581076fc16dbd..723639fd147705 100644
--- a/clang/tools/cir-translate/cir-translate.cpp
+++ b/clang/tools/cir-translate/cir-translate.cpp
@@ -85,12 +85,8 @@ std::string prepareCIRModuleTriple(mlir::ModuleOp mod) {
   return triple;
 }
 
-llvm::LogicalResult prepareCIRModuleDataLayout(mlir::ModuleOp mod,
-                                               llvm::StringRef rawTriple) {
-  auto *context = mod.getContext();
-
-  // Data layout is fully determined by the target triple. Here we only pass 
the
-  // triple to get the data layout.
+std::optional<std::string>
+getTargetDataLayoutString(llvm::StringRef rawTriple) {
   llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID(
       new clang::DiagnosticIDs);
   clang::DiagnosticOptions diagOpts;
@@ -103,21 +99,48 @@ llvm::LogicalResult 
prepareCIRModuleDataLayout(mlir::ModuleOp mod,
   targetOptions.Triple = rawTriple;
   llvm::IntrusiveRefCntPtr<clang::TargetInfo> targetInfo =
       clang::TargetInfo::CreateTargetInfo(*diagnostics, targetOptions);
-  if (!targetInfo) {
+  if (!targetInfo)
+    return std::nullopt;
+  return targetInfo->getDataLayoutString();
+}
+
+llvm::LogicalResult prepareCIRModuleDataLayout(mlir::ModuleOp mod,
+                                               llvm::StringRef rawTriple) {
+  auto *context = mod.getContext();
+
+  // Data layout is fully determined by the target triple. Here we only pass 
the
+  // triple to get the data layout.
+  std::optional<std::string> layoutString =
+      getTargetDataLayoutString(rawTriple);
+  if (!layoutString) {
     mod.emitError() << "error: invalid target triple '" << rawTriple << "'\n";
     return llvm::failure();
   }
-  std::string layoutString = targetInfo->getDataLayoutString();
 
   // Registered dialects may not be loaded yet, ensure they are.
   context->loadDialect<mlir::DLTIDialect, mlir::LLVM::LLVMDialect,
                        mlir::omp::OpenMPDialect>();
 
-  cir::setMLIRDataLayout(mod, llvm::DataLayout(layoutString));
+  cir::setMLIRDataLayout(mod, llvm::DataLayout(*layoutString));
 
   return llvm::success();
 }
 
+void canonicalizeDataLayout(llvm::Module &llvmModule) {
+  std::optional<std::string> targetLayout =
+      getTargetDataLayoutString(llvmModule.getTargetTriple().str());
+  if (!targetLayout)
+    return;
+  llvm::Expected<llvm::DataLayout> parsed =
+      llvm::DataLayout::parse(*targetLayout);
+  if (!parsed) {
+    llvm::consumeError(parsed.takeError());
+    return;
+  }
+  if (*parsed == llvmModule.getDataLayout())
+    llvmModule.setDataLayout(*targetLayout);
+}
+
 /// Prepare requirements like cir.triple and data layout.
 llvm::LogicalResult prepareCIRModuleForTranslation(mlir::ModuleOp mod) {
   auto modTriple = mod->getAttrOfType<mlir::StringAttr>(
@@ -172,6 +195,7 @@ void registerToLLVMTranslation() {
                                                       enableOpenMP);
         if (!llvmModule)
           return mlir::failure();
+        cir::canonicalizeDataLayout(*llvmModule);
         llvmModule->renumberMetadataForAssembly();
         llvmModule->print(output, nullptr);
         return mlir::success();

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to