[llvm] 2dea969 - [clang][CodeGen] Introduce `-frecord-command-line` for MachO
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 24 00:28:08 PDT 2023
Author: Antonio Frighetto
Date: 2023-07-24T09:24:59+02:00
New Revision: 2dea969d8337251e4c9335fd601bd4e0e5dee10f
URL: https://github.com/llvm/llvm-project/commit/2dea969d8337251e4c9335fd601bd4e0e5dee10f
DIFF: https://github.com/llvm/llvm-project/commit/2dea969d8337251e4c9335fd601bd4e0e5dee10f.diff
LOG: [clang][CodeGen] Introduce `-frecord-command-line` for MachO
Allow clang driver command-line recording when
targeting MachO object files as well.
Reviewed-by: sgraenitz
Differential Revision: https://reviews.llvm.org/D155716
Added:
llvm/test/CodeGen/AArch64/commandline-metadata.ll
Modified:
clang/lib/Driver/ToolChains/Clang.cpp
clang/test/Driver/clang_f_opts.c
llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Removed:
################################################################################
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index ef20dd5aefe7dc..adb550d9c5da50 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7183,7 +7183,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Args.hasFlag(options::OPT_frecord_command_line,
options::OPT_fno_record_command_line, false);
if (FRecordSwitches && !Triple.isOSBinFormatELF() &&
- !Triple.isOSBinFormatXCOFF())
+ !Triple.isOSBinFormatXCOFF() && !Triple.isOSBinFormatMachO())
D.Diag(diag::err_drv_unsupported_opt_for_target)
<< Args.getLastArg(options::OPT_frecord_command_line)->getAsString(Args)
<< TripleStr;
diff --git a/clang/test/Driver/clang_f_opts.c b/clang/test/Driver/clang_f_opts.c
index e8bc46e23b78a3..8a27140cd16e5f 100644
--- a/clang/test/Driver/clang_f_opts.c
+++ b/clang/test/Driver/clang_f_opts.c
@@ -541,7 +541,7 @@
// RUN: %clang -### -S -target x86_64-unknown-linux -fno-record-command-line -frecord-command-line %s 2>&1 | FileCheck -check-prefix=CHECK-RECORD-GCC-SWITCHES %s
// RUN: %clang -### -S -target x86_64-unknown-linux -frecord-command-line -fno-record-command-line %s 2>&1 | FileCheck -check-prefix=CHECK-NO-RECORD-GCC-SWITCHES %s
// Test with a couple examples of non-ELF object file formats
-// RUN: %clang -### -S -target x86_64-unknown-macosx -frecord-command-line %s 2>&1 | FileCheck -check-prefix=CHECK-RECORD-GCC-SWITCHES-ERROR %s
+// RUN: %clang -### -S -target x86_64-unknown-macosx -frecord-command-line %s 2>&1 | FileCheck -check-prefix=CHECK-RECORD-GCC-SWITCHES %s
// RUN: %clang -### -S -target x86_64-unknown-windows -frecord-command-line %s 2>&1 | FileCheck -check-prefix=CHECK-RECORD-GCC-SWITCHES-ERROR %s
// CHECK-RECORD-GCC-SWITCHES: "-record-command-line"
// CHECK-NO-RECORD-GCC-SWITCHES-NOT: "-record-command-line"
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 07ebf5e65431d3..9f92b919824d2d 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -156,6 +156,8 @@ class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
const TargetMachine &TM) const override;
+
+ MCSection *getSectionForCommandLines() const override;
};
class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 9e0914f667f02b..3994552884c4f6 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1420,6 +1420,11 @@ MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
return ReadOnlySection; // .const
}
+MCSection *TargetLoweringObjectFileMachO::getSectionForCommandLines() const {
+ return getContext().getMachOSection("__TEXT", "__command_line", 0,
+ SectionKind::getReadOnly());
+}
+
const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
MachineModuleInfo *MMI, MCStreamer &Streamer) const {
diff --git a/llvm/test/CodeGen/AArch64/commandline-metadata.ll b/llvm/test/CodeGen/AArch64/commandline-metadata.ll
new file mode 100644
index 00000000000000..7c6e01763220d1
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/commandline-metadata.ll
@@ -0,0 +1,24 @@
+; RUN: llc -mtriple=arm64-linux-gnu < %s | FileCheck %s
+; RUN: llc -mtriple=arm64-apple-darwin < %s | FileCheck %s --check-prefix=CHECK-MACHO
+
+; Verify that llvm.commandline metadata is emitted to the corresponding command line section.
+
+; CHECK: .text
+; CHECK: .section .GCC.command.line,"MS", at progbits,1
+; CHECK-NEXT: .zero 1
+; CHECK-NEXT: .ascii "clang -command1"
+; CHECK-NEXT: .zero 1
+; CHECK-NEXT: .ascii "clang -command2"
+; CHECK-NEXT: .zero 1
+
+; CHECK-MACHO: .section __TEXT,__text,regular,pure_instructions
+; CHECK-MACHO-NEXT: .section __TEXT,__command_line
+; CHECK-MACHO-NEXT: .space 1
+; CHECK-MACHO-NEXT: .ascii "clang -command1"
+; CHECK-MACHO-NEXT: .space 1
+; CHECK-MACHO-NEXT: .ascii "clang -command2"
+; CHECK-MACHO-NEXT: .space 1
+
+!llvm.commandline = !{!0, !1}
+!0 = !{!"clang -command1"}
+!1 = !{!"clang -command2"}
More information about the llvm-commits
mailing list