From 5472947e8b088203a989ca3750b094a5a61d5dc8 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Thu, 13 Nov 2025 14:47:16 -0500 Subject: [PATCH 1/8] Prevent loss of file type flags when creating temporary --- llvm/lib/Support/VirtualOutputBackends.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp index de59b8ab63a53..0fbb07d38c37c 100644 --- a/llvm/lib/Support/VirtualOutputBackends.cpp +++ b/llvm/lib/Support/VirtualOutputBackends.cpp @@ -269,9 +269,16 @@ Error OnDiskOutputFile::tryToCreateTemporary(std::optional &FD) { return createDirectoriesOnDemand(OutputPath, Config, [&]() -> Error { int NewFD; SmallString<128> UniquePath; + sys::fs::OpenFlags OF = sys::fs::OF_None; + if (Config.getTextWithCRLF()) + OF |= sys::fs::OF_TextWithCRLF; + else if (Config.getText()) + OF |= sys::fs::OF_Text; + if (Config.getAppend()) + OF |= sys::fs::OF_Append; if (std::error_code EC = - sys::fs::createUniqueFile(ModelPath, NewFD, UniquePath)) - return make_error(ModelPath, OutputPath, EC); + sys::fs::createUniqueFile(ModelPath, NewFD, UniquePath, OF)) + return make_error(ModelPath, OutputPath, EC); if (Config.getDiscardOnSignal()) sys::RemoveFileOnSignal(UniquePath); From 50a76863e1e42fbefcbbdc401bcb000b26f8bbc3 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Thu, 13 Nov 2025 14:52:22 -0500 Subject: [PATCH 2/8] syntax fix --- llvm/lib/Support/VirtualOutputBackends.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp index 0fbb07d38c37c..f44cfd419f8c7 100644 --- a/llvm/lib/Support/VirtualOutputBackends.cpp +++ b/llvm/lib/Support/VirtualOutputBackends.cpp @@ -278,7 +278,7 @@ Error OnDiskOutputFile::tryToCreateTemporary(std::optional &FD) { OF |= sys::fs::OF_Append; if (std::error_code EC = sys::fs::createUniqueFile(ModelPath, NewFD, UniquePath, OF)) - return make_error(ModelPath, OutputPath, EC); + return make_error(ModelPath, OutputPath, EC); if (Config.getDiscardOnSignal()) sys::RemoveFileOnSignal(UniquePath); From ba2c4217216c8d1e85a15f34329704920e0578b3 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Thu, 13 Nov 2025 17:20:20 -0500 Subject: [PATCH 3/8] add test for output encoding and consolidate code --- clang/test/CodeGen/SystemZ/encoding.c | 9 +++++++ llvm/lib/Support/VirtualOutputBackends.cpp | 28 +++++++++++----------- 2 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 clang/test/CodeGen/SystemZ/encoding.c diff --git a/clang/test/CodeGen/SystemZ/encoding.c b/clang/test/CodeGen/SystemZ/encoding.c new file mode 100644 index 0000000000000..d018a9c37852a --- /dev/null +++ b/clang/test/CodeGen/SystemZ/encoding.c @@ -0,0 +1,9 @@ +// Checks encoding of output file +// This is only required for z/OS. +// +// REQUIRES: system-zos, systemz-registered-target +// RUN: %clang_cc1 -triple s390x-ibm-zos -S %s -o %t.s +// RUN: ls -T %t.s | FileCheck %s + +// CHECK: t IBM-1047 T=on +void foo() { return; } diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp index f44cfd419f8c7..4fa37ccc0ad88 100644 --- a/llvm/lib/Support/VirtualOutputBackends.cpp +++ b/llvm/lib/Support/VirtualOutputBackends.cpp @@ -254,6 +254,18 @@ static Error createDirectoriesOnDemand(StringRef OutputPath, }); } +static sys::fs::OpenFlags generateFlagsFromConfig(OutputConfig Config) { + sys::fs::OpenFlags OF = sys::fs::OF_None; + if (Config.getTextWithCRLF()) + OF |= sys::fs::OF_TextWithCRLF; + else if (Config.getText()) + OF |= sys::fs::OF_Text; + if (Config.getAppend()) + OF |= sys::fs::OF_Append; + + return OF; +} + Error OnDiskOutputFile::tryToCreateTemporary(std::optional &FD) { // Create a temporary file. // Insert -%%%%%%%% before the extension (if any), and because some tools @@ -269,13 +281,7 @@ Error OnDiskOutputFile::tryToCreateTemporary(std::optional &FD) { return createDirectoriesOnDemand(OutputPath, Config, [&]() -> Error { int NewFD; SmallString<128> UniquePath; - sys::fs::OpenFlags OF = sys::fs::OF_None; - if (Config.getTextWithCRLF()) - OF |= sys::fs::OF_TextWithCRLF; - else if (Config.getText()) - OF |= sys::fs::OF_Text; - if (Config.getAppend()) - OF |= sys::fs::OF_Append; + sys::fs::OpenFlags OF = generateFlagsFromConfig(Config); if (std::error_code EC = sys::fs::createUniqueFile(ModelPath, NewFD, UniquePath, OF)) return make_error(ModelPath, OutputPath, EC); @@ -319,13 +325,7 @@ Error OnDiskOutputFile::initializeFile(std::optional &FD) { // Not using a temporary file. Open the final output file. return createDirectoriesOnDemand(OutputPath, Config, [&]() -> Error { int NewFD; - sys::fs::OpenFlags OF = sys::fs::OF_None; - if (Config.getTextWithCRLF()) - OF |= sys::fs::OF_TextWithCRLF; - else if (Config.getText()) - OF |= sys::fs::OF_Text; - if (Config.getAppend()) - OF |= sys::fs::OF_Append; + sys::fs::OpenFlags OF = generateFlagsFromConfig(Config); if (std::error_code EC = sys::fs::openFileForWrite( OutputPath, NewFD, sys::fs::CD_CreateAlways, OF)) return convertToOutputError(OutputPath, EC); From 6a1a42a1b962b908b7ddf8b7eed791d005170cbf Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Thu, 13 Nov 2025 17:26:31 -0500 Subject: [PATCH 4/8] formatting --- llvm/lib/Support/VirtualOutputBackends.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp index 4fa37ccc0ad88..8bee870fff8ae 100644 --- a/llvm/lib/Support/VirtualOutputBackends.cpp +++ b/llvm/lib/Support/VirtualOutputBackends.cpp @@ -255,15 +255,15 @@ static Error createDirectoriesOnDemand(StringRef OutputPath, } static sys::fs::OpenFlags generateFlagsFromConfig(OutputConfig Config) { - sys::fs::OpenFlags OF = sys::fs::OF_None; - if (Config.getTextWithCRLF()) - OF |= sys::fs::OF_TextWithCRLF; - else if (Config.getText()) - OF |= sys::fs::OF_Text; - if (Config.getAppend()) - OF |= sys::fs::OF_Append; - - return OF; + sys::fs::OpenFlags OF = sys::fs::OF_None; + if (Config.getTextWithCRLF()) + OF |= sys::fs::OF_TextWithCRLF; + else if (Config.getText()) + OF |= sys::fs::OF_Text; + if (Config.getAppend()) + OF |= sys::fs::OF_Append; + + return OF; } Error OnDiskOutputFile::tryToCreateTemporary(std::optional &FD) { From ef66e2d41387042c8b3234b430c5a42428eae678 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Thu, 13 Nov 2025 17:32:07 -0500 Subject: [PATCH 5/8] formatting --- llvm/lib/Support/VirtualOutputBackends.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp index 8bee870fff8ae..603bbf60ceb4e 100644 --- a/llvm/lib/Support/VirtualOutputBackends.cpp +++ b/llvm/lib/Support/VirtualOutputBackends.cpp @@ -262,7 +262,7 @@ static sys::fs::OpenFlags generateFlagsFromConfig(OutputConfig Config) { OF |= sys::fs::OF_Text; if (Config.getAppend()) OF |= sys::fs::OF_Append; - + return OF; } From 97ce4a6cfd4bcf095b09836d935350b48c959ef5 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Fri, 14 Nov 2025 12:11:02 -0500 Subject: [PATCH 6/8] Fix test failure on Windows --- llvm/lib/Support/VirtualOutputBackends.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp index 603bbf60ceb4e..9b65f3fa9f4ee 100644 --- a/llvm/lib/Support/VirtualOutputBackends.cpp +++ b/llvm/lib/Support/VirtualOutputBackends.cpp @@ -260,7 +260,7 @@ static sys::fs::OpenFlags generateFlagsFromConfig(OutputConfig Config) { OF |= sys::fs::OF_TextWithCRLF; else if (Config.getText()) OF |= sys::fs::OF_Text; - if (Config.getAppend()) + if (Config.getAppend() && !Config.getAtomicWrite()) OF |= sys::fs::OF_Append; return OF; From bd041f5e994926cf3190eb8e967bb5845e012bdd Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Fri, 14 Nov 2025 12:35:28 -0500 Subject: [PATCH 7/8] add comment --- llvm/lib/Support/VirtualOutputBackends.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp index 9b65f3fa9f4ee..a223d606a7d3b 100644 --- a/llvm/lib/Support/VirtualOutputBackends.cpp +++ b/llvm/lib/Support/VirtualOutputBackends.cpp @@ -260,6 +260,8 @@ static sys::fs::OpenFlags generateFlagsFromConfig(OutputConfig Config) { OF |= sys::fs::OF_TextWithCRLF; else if (Config.getText()) OF |= sys::fs::OF_Text; + // Don't pass OF_Append if writting to temporary since OF_Append is + // not Atomic Append if (Config.getAppend() && !Config.getAtomicWrite()) OF |= sys::fs::OF_Append; From 42160c6ac1d5ab39aac6546315459a6ef7359750 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Fri, 14 Nov 2025 12:45:28 -0500 Subject: [PATCH 8/8] remove trailing space --- llvm/lib/Support/VirtualOutputBackends.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp index a223d606a7d3b..f9c8f1302e6c5 100644 --- a/llvm/lib/Support/VirtualOutputBackends.cpp +++ b/llvm/lib/Support/VirtualOutputBackends.cpp @@ -260,7 +260,7 @@ static sys::fs::OpenFlags generateFlagsFromConfig(OutputConfig Config) { OF |= sys::fs::OF_TextWithCRLF; else if (Config.getText()) OF |= sys::fs::OF_Text; - // Don't pass OF_Append if writting to temporary since OF_Append is + // Don't pass OF_Append if writting to temporary since OF_Append is // not Atomic Append if (Config.getAppend() && !Config.getAtomicWrite()) OF |= sys::fs::OF_Append;