[clang] [mlir] [SystemZ][CIR] Fix tests failing on z/OS (PR #246554)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 21:04:10 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-core

Author: Sean Perry (perry-ca)

<details>
<summary>Changes</summary>

The following tests are failing on z/OS.
```
CIR/CodeGen/call-conv-lowering-amdgpu-one-field-struct.c
CIR/CodeGen/call-conv-lowering-x86_64-no-noundef.cpp
CIR/CodeGen/call-conv-lowering-x86_64-rerun.c
CIR/CodeGen/cleanup-scope-throw-caught.cpp
CIR/CodeGen/cleanup-throw-from-cleanup.cpp
CIR/CodeGen/cleanup-throwing-dtor.cpp
CIR/CodeGen/invoke-attrs.cpp
CIR/CodeGen/loop-hints.cpp
CIR/CodeGen/lowering-prepare-poison-aggregate-rerun.c
CIR/CodeGen/switch_flat_op.cpp
CIR/CodeGen/try-catch-all-with-cleanup.cpp
CIR/CodeGen/try-catch-non-trivial-copy.cpp
CIR/CodeGen/virtual-fn-calls-eh.cpp
CIR/CodeGenCoroutines/coro-await-suspend-bool.cpp
CIR/CodeGenCoroutines/coro-fallthrough.cpp
CIR/CodeGenCoroutines/coro-task.cpp
CIR/CodeGenCoroutines/coro-throw.cpp
CIR/CodeGenHIP/cleanup-alloca-addrspace.hip
CIR/Tools/cir-offload-merge.cir
CIR/Transforms/mem2reg-regions.c
```
These are failing because the file I/O is being done in binary.  Handle the input as text and output as text or binary depending on the byte code option.  This will make cir-opt handle the IR files the same way that opt does.

```
CIR/emit-actions.cpp
```
Use od option `-tx1` instead of `-c ` to prevent conversion to EBCDIC characters.  Also add `--ignore-case` to the FileCheck so it handles different cases in hex values across platforms.

```
CIR/CodeGen/dumb-record.cpp
```
Had multiple streams writing to `llvm::outs`.  Differences in buffering across platforms resulted in wrong ordering in output.  Add a flush to ensure correct ordering.

---
Full diff: https://github.com/llvm/llvm-project/pull/246554.diff


7 Files Affected:

- (modified) clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp (+1) 
- (modified) clang/test/CIR/emit-actions.cpp (+2-2) 
- (modified) clang/tools/cir-offload-merge/cir-offload-merge.cpp (+1-1) 
- (modified) mlir/include/mlir/Support/FileUtilities.h (+3-1) 
- (modified) mlir/lib/Parser/Parser.cpp (+2-1) 
- (modified) mlir/lib/Support/FileUtilities.cpp (+6-4) 
- (modified) mlir/lib/Tools/mlir-opt/MlirOptMain.cpp (+2-1) 


``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
index 6b9b231e17be3d..0416959ce08b6b 100644
--- a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
@@ -994,6 +994,7 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *rd, cir::RecordType *ty) {
     rd->dump(llvm::outs());
     llvm::outs() << "\nLayout: ";
     rl->print(llvm::outs());
+    llvm::outs().flush();
   }
 
   // TODO: implement verification
diff --git a/clang/test/CIR/emit-actions.cpp b/clang/test/CIR/emit-actions.cpp
index 2e9e4d961dfabc..dd324668052111 100644
--- a/clang/test/CIR/emit-actions.cpp
+++ b/clang/test/CIR/emit-actions.cpp
@@ -7,7 +7,7 @@
 // RUN: llvm-objdump -t %t.o | FileCheck %s -check-prefix=OBJ
 
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir-bc %s -o %t.cirbc
-// RUN: od -An -c -N4 %t.cirbc | FileCheck %s --check-prefix=CIRBC-MAGIC
+// RUN: od -An -tx1 -N4 %t.cirbc | FileCheck %s --ignore-case --check-prefix=CIRBC-MAGIC
 // RUN: not grep -q "cir.global" %t.cirbc
 // RUN: cir-opt %t.cirbc | FileCheck %s -check-prefix=CIRBC
 
@@ -31,6 +31,6 @@ int x = 1;
 // OBJ: .data
 // OBJ-SAME: x
 
-// CIRBC-MAGIC: M L 357 R
+// CIRBC-MAGIC: 4d  4c  ef  52
 
 // CIRBC: cir.global {{.*}}@x = #cir.int<1> : !s32i
diff --git a/clang/tools/cir-offload-merge/cir-offload-merge.cpp b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
index cde2cd28345fe9..ac437bc0357eac 100644
--- a/clang/tools/cir-offload-merge/cir-offload-merge.cpp
+++ b/clang/tools/cir-offload-merge/cir-offload-merge.cpp
@@ -151,7 +151,7 @@ static int writeModuleToOutput(mlir::ModuleOp moduleOp,
   assert(!moduleOp->getParentOp() && "expected a top-level module");
   std::string errorMessage;
   std::unique_ptr<llvm::ToolOutputFile> outputFile =
-      mlir::openOutputFile(outputFileName, &errorMessage);
+      mlir::openOutputFile(outputFileName, &errorMessage, /*IsText=*/true);
   if (!outputFile) {
     mlir::emitError(moduleOp.getLoc(), errorMessage);
     return 1;
diff --git a/mlir/include/mlir/Support/FileUtilities.h b/mlir/include/mlir/Support/FileUtilities.h
index fca17d82a2bf2b..4d572dabb1a0f0 100644
--- a/mlir/include/mlir/Support/FileUtilities.h
+++ b/mlir/include/mlir/Support/FileUtilities.h
@@ -39,9 +39,11 @@ openInputFile(llvm::StringRef inputFilename, llvm::Align alignment,
 
 /// Open the file specified by its name for writing. Write the error message to
 /// `errorMessage` if errors occur and `errorMessage` is not nullptr.
+/// `isText` is true the file is opened in text mode; the default is
+/// binary.
 std::unique_ptr<llvm::ToolOutputFile>
 openOutputFile(llvm::StringRef outputFilename,
-               std::string *errorMessage = nullptr);
+               std::string *errorMessage = nullptr, bool isText = false);
 
 } // namespace mlir
 
diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp
index 310680bcfb34f4..f20502f3242144 100644
--- a/mlir/lib/Parser/Parser.cpp
+++ b/mlir/lib/Parser/Parser.cpp
@@ -79,7 +79,8 @@ static LogicalResult loadSourceFileBuffer(llvm::StringRef filename,
     return emitError(mlir::UnknownLoc::get(ctx),
                      "only main buffer parsed at the moment");
   }
-  auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(filename);
+  auto fileOrErr =
+      llvm::MemoryBuffer::getFileOrSTDIN(filename, /*IsText=*/true);
   if (fileOrErr.getError())
     return emitError(mlir::UnknownLoc::get(ctx),
                      "could not open input file " + filename);
diff --git a/mlir/lib/Support/FileUtilities.cpp b/mlir/lib/Support/FileUtilities.cpp
index a5c44d3a9c8eb0..c18dc7013637c9 100644
--- a/mlir/lib/Support/FileUtilities.cpp
+++ b/mlir/lib/Support/FileUtilities.cpp
@@ -23,7 +23,7 @@ static std::unique_ptr<llvm::MemoryBuffer>
 openInputFileImpl(StringRef inputFilename, std::string *errorMessage,
                   std::optional<llvm::Align> alignment) {
   auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(
-      inputFilename, /*IsText=*/false, /*RequiresNullTerminator=*/true,
+      inputFilename, /*IsText=*/true, /*RequiresNullTerminator=*/true,
       alignment);
   if (std::error_code error = fileOrErr.getError()) {
     if (errorMessage)
@@ -46,10 +46,12 @@ mlir::openInputFile(llvm::StringRef inputFilename, llvm::Align alignment,
 }
 
 std::unique_ptr<llvm::ToolOutputFile>
-mlir::openOutputFile(StringRef outputFilename, std::string *errorMessage) {
+mlir::openOutputFile(StringRef outputFilename, std::string *errorMessage,
+                     bool IsText) {
   std::error_code error;
-  auto result = std::make_unique<llvm::ToolOutputFile>(outputFilename, error,
-                                                       llvm::sys::fs::OF_None);
+  auto result = std::make_unique<llvm::ToolOutputFile>(
+      outputFilename, error,
+      IsText ? llvm::sys::fs::OF_Text : llvm::sys::fs::OF_None);
   if (error) {
     if (errorMessage)
       *errorMessage = "cannot open output file '" + outputFilename.str() +
diff --git a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
index 0a564a5055cfdf..bbdcf9deb18636 100644
--- a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
+++ b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
@@ -869,7 +869,8 @@ LogicalResult mlir::MlirOptMain(int argc, char **argv,
     return failure();
   }
 
-  auto output = openOutputFile(outputFilename, &errorMessage);
+  auto output = openOutputFile(outputFilename, &errorMessage,
+                               /*isText=*/!config.shouldEmitBytecode());
   if (!output) {
     llvm::errs() << errorMessage << "\n";
     return failure();

``````````

</details>


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


More information about the cfe-commits mailing list