2828#include " llvm/Support/Process.h"
2929#include " llvm/Support/TargetParser.h"
3030#include " llvm/Support/YAMLParser.h"
31+ #include " llvm/Option/ArgList.h"
3132
3233#ifdef LLVM_ON_UNIX
3334#include < unistd.h> // For getuid().
@@ -44,12 +45,14 @@ void FlangFrontend::ConstructJob(Compilation &C, const JobAction &JA,
4445 ArgStringList CommonCmdArgs;
4546 ArgStringList UpperCmdArgs;
4647 ArgStringList LowerCmdArgs;
47- SmallString<256 > Stem;
48+ SmallString<256 > Stem, Path ;
4849 std::string OutFile;
4950 bool NeedIEEE = false ;
5051 bool NeedFastMath = false ;
5152 bool NeedRelaxedMath = false ;
5253
54+ bool IsOpenMPDevice = JA.isDeviceOffloading (Action::OFK_OpenMP);
55+
5356 // Check number of inputs for sanity. We need at least one input.
5457 assert (Inputs.size () >= 1 && " Must have at least one input." );
5558
@@ -792,6 +795,22 @@ void FlangFrontend::ConstructJob(Compilation &C, const JobAction &JA,
792795 UpperCmdArgs.push_back (" -output" );
793796 UpperCmdArgs.push_back (ILMFile);
794797
798+ if (Args.getAllArgValues (options::OPT_fopenmp_targets_EQ).size () > 0 ) {
799+ SmallString<128 > TargetInfo;
800+ Path = llvm::sys::path::parent_path (Output.getFilename ());
801+ Arg* Tgts = Args.getLastArg (options::OPT_fopenmp_targets_EQ);
802+ assert (Tgts && Tgts->getNumValues () &&
803+ " OpenMP offloading has to have targets specified." );
804+ for (unsigned i = 0 ; i < Tgts->getNumValues (); ++i) {
805+ if (i)
806+ TargetInfo += ' ,' ;
807+ llvm::Triple T (Tgts->getValue (i));
808+ TargetInfo += T.getTriple ();
809+ }
810+ UpperCmdArgs.push_back (" -fopenmp-targets" );
811+ UpperCmdArgs.push_back (Args.MakeArgString (TargetInfo.str ()));
812+ }
813+
795814 C.addCommand (llvm::make_unique<Command>(JA, *this , UpperExec, UpperCmdArgs, Inputs));
796815
797816 // For -fsyntax-only or -E that is it
@@ -899,7 +918,49 @@ void FlangFrontend::ConstructJob(Compilation &C, const JobAction &JA,
899918 LowerCmdArgs.push_back (" -stbfile" );
900919 LowerCmdArgs.push_back (STBFile);
901920
902- LowerCmdArgs.push_back (" -asm" ); LowerCmdArgs.push_back (Args.MakeArgString (OutFile));
921+
922+ /* OpenMP GPU Offload */
923+ if (Args.getAllArgValues (options::OPT_fopenmp_targets_EQ).size () > 0 ) {
924+ // if (isa<CompileJobAction>(JA) && JA.isHostOffloading(Action::OFK_OpenMP)) {
925+ SmallString<128 > TargetInfo;// ("-fopenmp-targets ");
926+ SmallString<256 > TargetInfoAsm;// ("-fopenmp-targets-asm ");
927+ Path = llvm::sys::path::parent_path (Output.getFilename ());
928+
929+ Arg* Tgts = Args.getLastArg (options::OPT_fopenmp_targets_EQ);
930+ assert (Tgts && Tgts->getNumValues () &&
931+ " OpenMP offloading has to have targets specified." );
932+ for (unsigned i = 0 ; i < Tgts->getNumValues (); ++i) {
933+ if (i)
934+ TargetInfo += ' ,' ;
935+ // We need to get the string from the triple because it may be not exactly
936+ // the same as the one we get directly from the arguments.
937+ llvm::Triple T (Tgts->getValue (i));
938+ TargetInfo += T.getTriple ();
939+ // We also need to give a output file
940+ TargetInfoAsm += Path;
941+ TargetInfoAsm += " /" ;
942+ TargetInfoAsm += Stem;
943+ TargetInfoAsm += " -" ;
944+ TargetInfoAsm += T.getTriple ();
945+ TargetInfoAsm += " .ll" ;
946+ }
947+ LowerCmdArgs.push_back (" -fopenmp-targets" );
948+ LowerCmdArgs.push_back (Args.MakeArgString (TargetInfo.str ()));
949+ if (IsOpenMPDevice) {
950+ LowerCmdArgs.push_back (" -fopenmp-targets-asm" );
951+ LowerCmdArgs.push_back (Args.MakeArgString (OutFile));
952+ LowerCmdArgs.push_back (" -asm" );
953+ LowerCmdArgs.push_back (Args.MakeArgString (TargetInfoAsm.str ()));
954+ } else {
955+ LowerCmdArgs.push_back (" -fopenmp-targets-asm" );
956+ LowerCmdArgs.push_back (Args.MakeArgString (TargetInfoAsm.str ()));
957+ LowerCmdArgs.push_back (" -asm" );
958+ LowerCmdArgs.push_back (Args.MakeArgString (OutFile));
959+ }
960+ } else {
961+ LowerCmdArgs.push_back (" -asm" );
962+ LowerCmdArgs.push_back (Args.MakeArgString (OutFile));
963+ }
903964
904965 C.addCommand (llvm::make_unique<Command>(JA, *this , LowerExec, LowerCmdArgs, Inputs));
905966}
0 commit comments