1414// target-specific device code.
1515// ===---------------------------------------------------------------------===//
1616
17- #include " clang/Basic/Cuda .h"
17+ #include " clang/Basic/OffloadArch .h"
1818#include " clang/Basic/Version.h"
1919
2020#include " llvm/ADT/StringExtras.h"
@@ -72,11 +72,9 @@ static StringRef OutputFile;
7272// / Directory to dump SPIR-V IR if requested by user.
7373static SmallString<128 > SPIRVDumpDir;
7474
75- <<<<<<< HEAD
7675static bool IsAOTCompileNeeded = false ;
77- =======
76+
7877using OffloadingImage = OffloadBinary::OffloadingImage;
79- >>>>>>> llvm/main
8078
8179static void printVersion (raw_ostream &OS) {
8280 OS << clang::getClangToolFullVersion (" clang-sycl-linker" ) << ' \n ' ;
@@ -363,32 +361,25 @@ static Error runSPIRVCodeGen(StringRef File, const ArgList &Args,
363361 M->setDataLayout (TM->createDataLayout ());
364362
365363 // Open output file for writing.
366- Expected<StringRef> OutFileOrErr =
367- IsAOTCompileNeeded
368- ? createTempFile (Args, sys::path::filename (OutputFile), " spv" )
369- : OutputFile;
370- if (!OutFileOrErr)
371- return OutFileOrErr.takeError ();
372- std::error_code EC;
373- raw_fd_ostream OS (*OutFileOrErr, EC);
374- if (EC)
375- return createStringError (EC, " Could not open file " + *OutFileOrErr);
364+ int FD = -1 ;
365+ if (std::error_code EC = sys::fs::openFileForWrite (OutputFile, FD))
366+ return errorCodeToError (EC);
367+ auto OS = std::make_unique<llvm::raw_fd_ostream>(FD, true );
376368
377369 // Run SPIR-V codegen passes to generate SPIR-V file.
378370 legacy::PassManager CodeGenPasses;
379371 TargetLibraryInfoImpl TLII (M->getTargetTriple ());
380372 CodeGenPasses.add (new TargetLibraryInfoWrapperPass (TLII));
381- if (TM->addPassesToEmitFile (CodeGenPasses, OS, nullptr ,
373+ if (TM->addPassesToEmitFile (CodeGenPasses, * OS, nullptr ,
382374 CodeGenFileType::ObjectFile))
383375 return createStringError (" Failed to execute SPIR-V Backend" );
384376 CodeGenPasses.run (*M);
385377
386378 if (Verbose)
387379 errs () << formatv (" SPIR-V Backend: input: {0}, output: {1}\n " , File,
388- *OutFileOrErr );
380+ OutputFile );
389381
390- <<<<<<< HEAD
391- return *OutFileOrErr;
382+ return Error::success ();
392383}
393384
394385// / Run AOT compilation for Intel CPU.
@@ -397,7 +388,8 @@ static Error runSPIRVCodeGen(StringRef File, const ArgList &Args,
397388// / 'Args' encompasses all arguments required for linking and wrapping device
398389// / code and will be parsed to generate options required to be passed into the
399390// / SYCL AOT compilation step.
400- static Error runAOTCompileIntelCPU (StringRef InputFile, const ArgList &Args) {
391+ static Error runAOTCompileIntelCPU (StringRef InputFile, StringRef OutputFile,
392+ const ArgList &Args) {
401393 SmallVector<StringRef, 8 > CmdArgs;
402394 Expected<std::string> OpenCLAOTPath =
403395 findProgram (Args, " opencl-aot" , {getMainExecutable (" opencl-aot" )});
@@ -422,7 +414,8 @@ static Error runAOTCompileIntelCPU(StringRef InputFile, const ArgList &Args) {
422414// / 'Args' encompasses all arguments required for linking and wrapping device
423415// / code and will be parsed to generate options required to be passed into the
424416// / SYCL AOT compilation step.
425- static Error runAOTCompileIntelGPU (StringRef InputFile, const ArgList &Args) {
417+ static Error runAOTCompileIntelGPU (StringRef InputFile, StringRef OutputFile,
418+ const ArgList &Args) {
426419 SmallVector<StringRef, 8 > CmdArgs;
427420 Expected<std::string> OclocPath =
428421 findProgram (Args, " ocloc" , {getMainExecutable (" ocloc" )});
@@ -458,18 +451,16 @@ static Error runAOTCompileIntelGPU(StringRef InputFile, const ArgList &Args) {
458451// / 'Args' encompasses all arguments required for linking and wrapping device
459452// / code and will be parsed to generate options required to be passed into the
460453// / SYCL AOT compilation step.
461- static Error runAOTCompile (StringRef InputFile, const ArgList &Args) {
454+ static Error runAOTCompile (StringRef InputFile, StringRef OutputFile,
455+ const ArgList &Args) {
462456 StringRef Arch = Args.getLastArgValue (OPT_arch_EQ);
463457 OffloadArch OffloadArch = StringToOffloadArch (Arch);
464- if (IsIntelGPUArch (OffloadArch))
465- return runAOTCompileIntelGPU (InputFile, Args);
466- if (IsIntelCPUArch (OffloadArch))
467- return runAOTCompileIntelCPU (InputFile, Args);
458+ if (IsIntelGPUOffloadArch (OffloadArch))
459+ return runAOTCompileIntelGPU (InputFile, OutputFile, Args);
460+ if (IsIntelCPUOffloadArch (OffloadArch))
461+ return runAOTCompileIntelCPU (InputFile, OutputFile, Args);
468462
469463 return createStringError (inconvertibleErrorCode (), " Unsupported arch" );
470- =======
471- return Error::success ();
472- >>>>>>> llvm/main
473464}
474465
475466// / Performs the following steps:
@@ -492,26 +483,20 @@ Error runSYCLLink(ArrayRef<std::string> Files, const ArgList &Args) {
492483 SmallVector<std::string> SplitModules;
493484 SplitModules.emplace_back (*LinkedFile);
494485
495- // SPIR-V code generation step.
496- <<<<<<< HEAD
497- auto SPVFile = runSPIRVCodeGen (*LinkedFile, Args, C);
498- if (!SPVFile)
499- return SPVFile.takeError ();
500-
501- if (IsAOTCompileNeeded) {
502- if (Error Err = runAOTCompile (*SPVFile, Args))
503- return Err;
504- }
505-
506- =======
486+ // SPIR-V code generation step and AOT compilation step.
507487 for (size_t I = 0 , E = SplitModules.size (); I != E; ++I) {
508- auto Stem = OutputFile.rsplit (' .' ).first ;
509- std::string SPVFile (Stem);
510- SPVFile.append (" _" + utostr (I) + " .spv" );
511- auto Err = runSPIRVCodeGen (SplitModules[I], Args, SPVFile, C);
512- if (Err)
488+ StringRef Stem = OutputFile.rsplit (' .' ).first ;
489+ std::string SPVFile = (Stem + " _" + Twine (I) + " .spv" ).str ();
490+ if (Error Err = runSPIRVCodeGen (SplitModules[I], Args, SPVFile, C))
513491 return Err;
514- SplitModules[I] = SPVFile;
492+ if (!IsAOTCompileNeeded) {
493+ SplitModules[I] = SPVFile;
494+ } else {
495+ std::string AOTFile = (Stem + " _" + Twine (I) + " .out" ).str ();
496+ if (Error Err = runAOTCompile (SPVFile, AOTFile, Args))
497+ return Err;
498+ SplitModules[I] = AOTFile;
499+ }
515500 }
516501
517502 // Write the final output into file.
@@ -544,7 +529,6 @@ Error runSYCLLink(ArrayRef<std::string> Files, const ArgList &Args) {
544529 return createStringError (" Offload binary has invalid size alignment" );
545530 FS << Buffer;
546531 }
547- >>>>>>> llvm/main
548532 return Error::success ();
549533}
550534
@@ -586,20 +570,15 @@ int main(int argc, char **argv) {
586570 DryRun = Args.hasArg (OPT_dry_run);
587571 SaveTemps = Args.hasArg (OPT_save_temps);
588572
589- <<<<<<< HEAD
590- IsAOTCompileNeeded = Args.hasArg (OPT_arch_EQ);
573+ IsAOTCompileNeeded = IsIntelOffloadArch (
574+ StringToOffloadArch ( Args.getLastArgValue (OPT_arch_EQ)) );
591575
592576 if (!Args.hasArg (OPT_o))
593577 reportError (createStringError (" Output file must be specified" ));
594578 OutputFile = Args.getLastArgValue (OPT_o);
595579
596580 if (!Args.hasArg (OPT_triple_EQ))
597581 reportError (createStringError (" Target triple must be specified" ));
598- =======
599- OutputFile = " a.out" ;
600- if (Args.hasArg (OPT_o))
601- OutputFile = Args.getLastArgValue (OPT_o);
602- >>>>>>> llvm/main
603582
604583 if (Args.hasArg (OPT_spirv_dump_device_code_EQ)) {
605584 Arg *A = Args.getLastArg (OPT_spirv_dump_device_code_EQ);
0 commit comments