[clang] [clang] Allow MCTargetOptions to be parseable by -mllvm. (PR #66347)

Francesco Petrogalli via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 14 02:22:29 PDT 2023


https://github.com/fpetrogalli created https://github.com/llvm/llvm-project/pull/66347:

The cl::opt used by MCTargetOptions are not created until RegisterMCTargetOptionsFlags is instantiated. Due to this deferral the compiler driver -mllvm path is unable to parse flags such as --no-deprecated-warn which work properly in llvm-mc.

Move the instantiation into the frontend and cc1as and propagate the results into MCTargetOptions.

>From 72aab7cf85b7a2c09faf84ddd6a2d29af27834a7 Mon Sep 17 00:00:00 2001
From: Stephan Lachowsky <slachowsky at apple.com>
Date: Thu, 14 Sep 2023 11:11:19 +0200
Subject: [PATCH] [clang] Allow MCTargetOptions to be parseable by -mllvm.

The cl::opt used by MCTargetOptions are not created until
RegisterMCTargetOptionsFlags is instantiated. Due to this deferral the
compiler driver -mllvm path is unable to parse flags such as
--no-deprecated-warn which work properly in llvm-mc.

Move the instantiation into the frontend and cc1as and propagate the
results into MCTargetOptions.
---
 clang/test/Misc/cc1as-mllvm-mc-options.s    | 12 ++++++++++++
 clang/test/Misc/compiler-mllvm-mc-options.c | 12 ++++++++++++
 clang/tools/driver/cc1_main.cpp             |  4 ++++
 clang/tools/driver/cc1as_main.cpp           |  3 ++-
 4 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Misc/cc1as-mllvm-mc-options.s
 create mode 100644 clang/test/Misc/compiler-mllvm-mc-options.c

diff --git a/clang/test/Misc/cc1as-mllvm-mc-options.s b/clang/test/Misc/cc1as-mllvm-mc-options.s
new file mode 100644
index 000000000000000..189e3c546b8fd94
--- /dev/null
+++ b/clang/test/Misc/cc1as-mllvm-mc-options.s
@@ -0,0 +1,12 @@
+// Ensure MCTargetOptionsCommandFlags are parsable under -mllvm
+// RUN: %clang -cc1as -mllvm --help %s | FileCheck %s
+// CHECK: --asm-show-inst
+// CHECK: --dwarf-version
+// CHECK: --dwarf64
+// CHECK: --emit-dwarf-unwind
+// CHECK: --fatal-warnings
+// CHECK: --incremental-linker-compatible
+// CHECK: --mc-relax-all
+// CHECK: --no-deprecated-warn
+// CHECK: --no-type-check
+// CHECK: --no-warn
diff --git a/clang/test/Misc/compiler-mllvm-mc-options.c b/clang/test/Misc/compiler-mllvm-mc-options.c
new file mode 100644
index 000000000000000..10b2a1172968d66
--- /dev/null
+++ b/clang/test/Misc/compiler-mllvm-mc-options.c
@@ -0,0 +1,12 @@
+// Ensure MCTargetOptionsCommandFlags are parsable under -mllvm
+// RUN: %clang -mllvm --help -c %s -o /dev/null | FileCheck %s
+// CHECK: --asm-show-inst
+// CHECK: --dwarf-version
+// CHECK: --dwarf64
+// CHECK: --emit-dwarf-unwind
+// CHECK: --fatal-warnings
+// CHECK: --incremental-linker-compatible
+// CHECK: --mc-relax-all
+// CHECK: --no-deprecated-warn
+// CHECK: --no-type-check
+// CHECK: --no-warn
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index 9e7f8679b4cbdff..cc7089842a60c0d 100644
--- a/clang/tools/driver/cc1_main.cpp
+++ b/clang/tools/driver/cc1_main.cpp
@@ -28,6 +28,7 @@
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Config/llvm-config.h"
 #include "llvm/LinkAllPasses.h"
+#include "llvm/MC/MCTargetOptionsCommandFlags.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
@@ -53,6 +54,9 @@
 using namespace clang;
 using namespace llvm::opt;
 
+// Initialize MC Target option flags (for -mllvm)
+static llvm::mc::RegisterMCTargetOptionsFlags MOF;
+
 //===----------------------------------------------------------------------===//
 // Main driver
 //===----------------------------------------------------------------------===//
diff --git a/clang/tools/driver/cc1as_main.cpp b/clang/tools/driver/cc1as_main.cpp
index 3c5926073f026a6..3eb7c8c555bf186 100644
--- a/clang/tools/driver/cc1as_main.cpp
+++ b/clang/tools/driver/cc1as_main.cpp
@@ -36,6 +36,7 @@
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSubtargetInfo.h"
 #include "llvm/MC/MCTargetOptions.h"
+#include "llvm/MC/MCTargetOptionsCommandFlags.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
@@ -408,7 +409,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
   assert(MRI && "Unable to create target register info!");
 
-  MCTargetOptions MCOptions;
+  MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
   MCOptions.EmitDwarfUnwind = Opts.EmitDwarfUnwind;
   MCOptions.EmitCompactUnwindNonCanonical = Opts.EmitCompactUnwindNonCanonical;
   MCOptions.AsSecureLogFile = Opts.AsSecureLogFile;



More information about the cfe-commits mailing list