From 2affd76a6b67c16af522df4c8379fe8c22a90fe1 Mon Sep 17 00:00:00 2001
From: Simeon Ehrig <simeonehrig@web.de>
Date: Wed, 14 Mar 2018 13:41:11 +0100
Subject: [PATCH] reduce arguments at start of cling -xcling

the argument --cuda-host-only and the default c++ stardard will be set at start of cling -xcuda
---
 .../include/cling/Interpreter/InvocationOptions.h |  1 +
 interpreter/cling/lib/Interpreter/CIFactory.cpp   |  8 ++++++++
 .../cling/lib/Interpreter/InvocationOptions.cpp   |  8 +++++---
 .../cling/test/Interfaces/invocationFlags.C       | 15 ++++++++++++++-
 4 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/interpreter/cling/include/cling/Interpreter/InvocationOptions.h b/interpreter/cling/include/cling/Interpreter/InvocationOptions.h
index 3304e2ca415..833d139284e 100644
--- a/interpreter/cling/include/cling/Interpreter/InvocationOptions.h
+++ b/interpreter/cling/include/cling/Interpreter/InvocationOptions.h
@@ -58,6 +58,7 @@ namespace cling {
     unsigned HasOutput : 1;
     unsigned Verbose : 1;
     unsigned CxxModules : 1;
+    unsigned CUDA : 1;
     /// \brief The output path of any C++ PCMs we're building on demand.
     /// Equal to ModuleCachePath in the HeaderSearchOptions.
     std::string CachePath;
diff --git a/interpreter/cling/lib/Interpreter/CIFactory.cpp b/interpreter/cling/lib/Interpreter/CIFactory.cpp
index f08997196a1..a211f746b4d 100644
--- a/interpreter/cling/lib/Interpreter/CIFactory.cpp
+++ b/interpreter/cling/lib/Interpreter/CIFactory.cpp
@@ -885,6 +885,7 @@ static void stringifyPreprocSetting(PreprocessorOptions& PPOpts,
       argvCompile.push_back("-x");
       argvCompile.push_back( "c++");
     }
+
     if (COpts.DefaultLanguage()) {
       // By default, set the standard to what cling was compiled with.
       // clang::driver::Compilation will do various things while initializing
@@ -897,6 +898,13 @@ static void stringifyPreprocSetting(PreprocessorOptions& PPOpts,
         default: llvm_unreachable("Unrecognized C++ version");
       }
     }
+
+    // This argument starts the cling instance with the x86 target. Otherwise,
+    // the first job in the joblist starts the cling instance with the nvptx
+    // target.
+    if(COpts.CUDA)
+      argvCompile.push_back("--cuda-host-only");
+
     // argv[0] already inserted, get the rest
     argvCompile.insert(argvCompile.end(), argv+1, argv + argc);
 
diff --git a/interpreter/cling/lib/Interpreter/InvocationOptions.cpp b/interpreter/cling/lib/Interpreter/InvocationOptions.cpp
index f3d7123e6db..77dd965a42b 100644
--- a/interpreter/cling/lib/Interpreter/InvocationOptions.cpp
+++ b/interpreter/cling/lib/Interpreter/InvocationOptions.cpp
@@ -100,7 +100,7 @@ static const char kNoStdInc[] = "-nostdinc";
 CompilerOptions::CompilerOptions(int argc, const char* const* argv)
     : Language(false), ResourceDir(false), SysRoot(false), NoBuiltinInc(false),
       NoCXXInc(false), StdVersion(false), StdLib(false), HasOutput(false),
-      Verbose(false), CxxModules(false) {
+      Verbose(false), CxxModules(false), CUDA(false) {
   if (argc && argv) {
     // Preserve what's already in Remaining, the user might want to push args
     // to clang while still using main's argc, argv
@@ -126,7 +126,9 @@ void CompilerOptions::Parse(int argc, const char* const argv[],
       // case options::OPT_d_Flag:
       case options::OPT_E:
       case options::OPT_o: HasOutput = true; break;
-      case options::OPT_x: Language = true; break;
+      case options::OPT_x: Language = true;
+                           CUDA = llvm::StringRef(arg->getValue()) == "cuda";
+                           break;
       case options::OPT_resource_dir: ResourceDir = true; break;
       case options::OPT_isysroot: SysRoot = true; break;
       case options::OPT_std_EQ: StdVersion = true; break;
@@ -158,7 +160,7 @@ bool CompilerOptions::DefaultLanguage(const LangOptions* LangOpts) const {
   // Also don't set up the defaults when language is explicitly set; unless
   // the language was set to generate a PCH, in which case definitely do.
   if (Language)
-    return HasOutput || (LangOpts && LangOpts->CompilingPCH);
+    return HasOutput || (LangOpts && LangOpts->CompilingPCH) || CUDA;
 
   return true;
 }
diff --git a/interpreter/cling/test/Interfaces/invocationFlags.C b/interpreter/cling/test/Interfaces/invocationFlags.C
index fe252f2028b..6ba276d89a1 100644
--- a/interpreter/cling/test/Interfaces/invocationFlags.C
+++ b/interpreter/cling/test/Interfaces/invocationFlags.C
@@ -36,12 +36,19 @@ COpts.NoBuiltinInc
 // CHECK-NEXT: (unsigned int) 1
 COpts.NoCXXInc
 // CHECK-NEXT: (unsigned int) 0
+COpts.CUDA
+// CHECK-NEXT: (unsigned int) 0
+
+COpts.DefaultLanguage()
+// CHECK-NEXT: false
 
 // library caller options: arguments passed as is
 COpts.Remaining
 // CHECK-NEXT: {{.*}} { "progname", "-", "-xobjective-c", "FileToExecuteA", "-isysroot", "APAth", "-nobuiltininc", "-v", "FileToExecuteB", "-L/Path/To/Libs", "-lTest" }
 
+argv[2] = "-xcuda";
 argv[6] = "-nostdinc++";
+
 cling::InvocationOptions IOpts(argc, argv);
 IOpts.Inputs
 // CHECK-NEXT: {{.*}} { "-", "FileToExecuteA", "FileToExecuteB" }
@@ -60,12 +67,18 @@ IOpts.CompilerOpts.NoBuiltinInc
 // CHECK-NEXT: (unsigned int) 0
 IOpts.CompilerOpts.NoCXXInc
 // CHECK-NEXT: (unsigned int) 1
+IOpts.CompilerOpts.CUDA
+// CHECK-NEXT: (unsigned int) 1
+
+// if the language is cuda, it should set automatically the c++ standard
+IOpts.CompilerOpts.DefaultLanguage()
+// CHECK-NEXT: true
 
 // user options from main: filtered by cling (no '-')
 IOpts.CompilerOpts.Remaining
 
 // Windows translates -nostdinc++ to -nostdinc++. Ignore that fact for the test.
-// CHECK-NEXT: {{.*}} { "progname", "-xobjective-c", "FileToExecuteA", "-isysroot", "APAth", {{.*}}, "-v", "FileToExecuteB" }
+// CHECK-NEXT: {{.*}} { "progname", "-xcuda", "FileToExecuteA", "-isysroot", "APAth", {{.*}}, "-v", "FileToExecuteB" }
 
 // expected-no-diagnostics
 .q
-- 
GitLab