python3kgae created this revision.
python3kgae added reviewers: amccarth, craig.topper, hans, rnk, 
stefan_reinalter, beanz, pow2clk.
Herald added subscribers: Anastasia, StephenFan.
Herald added a project: All.
python3kgae requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay.
Herald added a project: clang.

A new option -P is added for dxc mode.
The format is -P proprecossed_file_name
It will make compile preprocess only and output to proprecossed_file_name.

Internally it will be translated into -E -o proprecossed_file_name.

When -P exists, other output compile option like -Fo will be ignored because 
compiler will run preprocess only.
A warning will be report if this happened.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125655

Files:
  clang/include/clang/Basic/DiagnosticDriverKinds.td
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/Driver.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/HLSL.cpp
  clang/test/Driver/dxc_P.hlsl

Index: clang/test/Driver/dxc_P.hlsl
===================================================================
--- /dev/null
+++ clang/test/Driver/dxc_P.hlsl
@@ -0,0 +1,11 @@
+// RUN: %clang_dxc  -P a.txt -### %s 2>&1 | FileCheck %s
+// RUN: %clang_dxc  -Fo b.txt -P a.txt -### %s 2>&1 | FileCheck %s --check-prefix=WARNING
+
+// Make sure -P option flag which translated into "-E" + "-o" "a.txt".
+// CHECK:"-E" {{.*}} "-o" "a.txt"
+
+// Make sure got warning when -Fo and -P at same time.
+// WARNING: warning: output compiler options like -Fo ignored with Preprocess [-Woption-ignored]
+// WARNING:"-E" {{.*}} "-o" "a.txt"
+
+
Index: clang/lib/Driver/ToolChains/HLSL.cpp
===================================================================
--- clang/lib/Driver/ToolChains/HLSL.cpp
+++ clang/lib/Driver/ToolChains/HLSL.cpp
@@ -165,7 +165,6 @@
   for (Arg *A : Args) {
     if (A->getOption().getID() == options::OPT_dxil_validator_version) {
       StringRef ValVerStr = A->getValue();
-      std::string ErrorMsg;
       if (!isLegalValidatorVersion(ValVerStr, getDriver()))
         continue;
     }
@@ -178,6 +177,15 @@
       A->claim();
       continue;
     }
+    if (A->getOption().getID() == options::OPT_dxc_P) {
+      // Translate dxc_P into -E and -o.
+      DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_E));
+      // The -o part is done at clang::driver::Driver::GetNamedOutputPath.
+      A->claim();
+      if (Args.getLastArg(options::OPT_o))
+        getDriver().Diag(diag::warn_drv_dxc_ignore_output_for_preprocess);
+      continue;
+    }
     DAL->append(A);
   }
   // Add default validator version if not set.
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3475,9 +3475,9 @@
 
 static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
                               types::ID InputType) {
-  const unsigned ForwardedArguments[] = {options::OPT_dxil_validator_version,
-                                         options::OPT_S, options::OPT_emit_llvm,
-                                         options::OPT_disable_llvm_passes};
+  const unsigned ForwardedArguments[] = {
+      options::OPT_dxil_validator_version, options::OPT_E, options::OPT_S,
+      options::OPT_emit_llvm, options::OPT_disable_llvm_passes};
 
   for (const auto &Arg : ForwardedArguments)
     if (const auto *A = Args.getLastArg(Arg))
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -5507,6 +5507,12 @@
   }
 
   llvm::PrettyStackTraceString CrashInfo("Computing output path");
+
+  // For dxc_P,Output to user requested destination.
+  // When dxc_P is set, -Fo (which is aliased to OPT_o) will be ignored.
+  if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_dxc_P))
+    return C.addResultFile(FinalOutput->getValue(), &JA);
+
   // Output to a user requested destination?
   if (AtTopLevel && !isa<DsymutilJobAction>(JA) && !isa<VerifyJobAction>(JA)) {
     if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -6790,3 +6790,7 @@
   HelpText<"Emit pristine LLVM IR from the frontend by not running any LLVM passes at all."
            "Same as -S + -emit-llvm + -disable-llvm-passes.">;
 def fcgl : DXCFlag<"fcgl">, Alias<emit_pristine_llvm>;
+def dxc_P : Option<["--", "/", "-"], "P", KIND_SEPARATE>,
+  Group<dxc_Group>, Flags<[DXCOption, NoXarchOption]>,
+  HelpText<"Preprocess to file (must be used alone)."
+           "Same as -E + -o.">;
Index: clang/include/clang/Basic/DiagnosticDriverKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -680,4 +680,7 @@
 def err_drv_invalid_empty_dxil_validator_version : Error<
   "invalid validator version : %0\n"
   "If validator major version is 0, minor version must also be 0.">;
+def warn_drv_dxc_ignore_output_for_preprocess : Warning<
+  "output compiler options like -Fo ignored with Preprocess">,
+  InGroup<OptionIgnored>;
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to