18
18
#include " llvm/Support/VirtualFileSystem.h"
19
19
#ifdef __EMSCRIPTEN__
20
20
#include " Wasm.h"
21
+ #include < dlfcn.h>
21
22
#endif // __EMSCRIPTEN__
22
23
23
24
#include " clang/AST/ASTConsumer.h"
@@ -480,20 +481,34 @@ Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI,
480
481
OverlayVFS->pushOverlay (IMVFS);
481
482
CI->createFileManager (OverlayVFS);
482
483
483
- auto Interp = Interpreter::create (std::move (CI));
484
- if (auto E = Interp.takeError ())
485
- return std::move (E);
484
+ llvm::Expected<std::unique_ptr<Interpreter>> InterpOrErr =
485
+ Interpreter::create (std::move (CI));
486
+ if (!InterpOrErr)
487
+ return InterpOrErr;
488
+
489
+ std::unique_ptr<Interpreter> Interp = std::move (*InterpOrErr);
486
490
487
491
llvm::Error Err = llvm::Error::success ();
488
- auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
489
- std::move (DCI), *(*Interp)->getCompilerInstance (), IMVFS, Err,
490
- (*Interp)->PTUs );
492
+ llvm::LLVMContext &LLVMCtx = *Interp->TSCtx ->getContext ();
493
+
494
+ auto DeviceAct =
495
+ std::make_unique<IncrementalAction>(*DCI, LLVMCtx, Err, *Interp);
496
+
491
497
if (Err)
492
498
return std::move (Err);
493
499
494
- (*Interp)->DeviceParser = std::move (DeviceParser);
500
+ Interp->DeviceAct = std::move (DeviceAct);
501
+
502
+ DCI->ExecuteAction (*Interp->DeviceAct );
503
+
504
+ auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
505
+ std::move (DCI), *Interp->getCompilerInstance (), IMVFS, Err, Interp->PTUs );
506
+
507
+ if (Err)
508
+ return std::move (Err);
495
509
496
- return Interp;
510
+ Interp->DeviceParser = std::move (DeviceParser);
511
+ return std::move (Interp);
497
512
}
498
513
499
514
const CompilerInstance *Interpreter::getCompilerInstance () const {
@@ -531,15 +546,17 @@ size_t Interpreter::getEffectivePTUSize() const {
531
546
532
547
PartialTranslationUnit &
533
548
Interpreter::RegisterPTU (TranslationUnitDecl *TU,
534
- std::unique_ptr<llvm::Module> M /* ={}*/ ) {
549
+ std::unique_ptr<llvm::Module> M /* ={}*/ ,
550
+ IncrementalAction *Action) {
535
551
PTUs.emplace_back (PartialTranslationUnit ());
536
552
PartialTranslationUnit &LastPTU = PTUs.back ();
537
553
LastPTU.TUPart = TU;
538
554
539
555
if (!M)
540
- M = GenModule ();
556
+ M = GenModule (Action );
541
557
542
- assert ((!getCodeGen () || M) && " Must have a llvm::Module at this point" );
558
+ assert ((!getCodeGen (Action) || M) &&
559
+ " Must have a llvm::Module at this point" );
543
560
544
561
LastPTU.TheModule = std::move (M);
545
562
LLVM_DEBUG (llvm::dbgs () << " compile-ptu " << PTUs.size () - 1
@@ -559,6 +576,16 @@ Interpreter::Parse(llvm::StringRef Code) {
559
576
llvm::Expected<TranslationUnitDecl *> DeviceTU = DeviceParser->Parse (Code);
560
577
if (auto E = DeviceTU.takeError ())
561
578
return std::move (E);
579
+
580
+ RegisterPTU (*DeviceTU, nullptr , DeviceAct.get ());
581
+
582
+ llvm::Expected<llvm::StringRef> PTX = DeviceParser->GeneratePTX ();
583
+ if (!PTX)
584
+ return PTX.takeError ();
585
+
586
+ llvm::Error Err = DeviceParser->GenerateFatbinary ();
587
+ if (Err)
588
+ return std::move (Err);
562
589
}
563
590
564
591
// Tell the interpreter sliently ignore unused expressions since value
@@ -711,6 +738,14 @@ llvm::Error Interpreter::Undo(unsigned N) {
711
738
}
712
739
713
740
llvm::Error Interpreter::LoadDynamicLibrary (const char *name) {
741
+ #ifdef __EMSCRIPTEN__
742
+ void *handle = dlopen (name, RTLD_NOW | RTLD_GLOBAL);
743
+ if (!handle) {
744
+ llvm::errs () << dlerror () << ' \n ' ;
745
+ return llvm::make_error<llvm::StringError>(" Failed to load dynamic library" ,
746
+ llvm::inconvertibleErrorCode ());
747
+ }
748
+ #else
714
749
auto EE = getExecutionEngine ();
715
750
if (!EE)
716
751
return EE.takeError ();
@@ -722,13 +757,15 @@ llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
722
757
EE->getMainJITDylib ().addGenerator (std::move (*DLSG));
723
758
else
724
759
return DLSG.takeError ();
760
+ #endif
725
761
726
762
return llvm::Error::success ();
727
763
}
728
764
729
- std::unique_ptr<llvm::Module> Interpreter::GenModule () {
765
+ std::unique_ptr<llvm::Module>
766
+ Interpreter::GenModule (IncrementalAction *Action) {
730
767
static unsigned ID = 0 ;
731
- if (CodeGenerator *CG = getCodeGen ()) {
768
+ if (CodeGenerator *CG = getCodeGen (Action )) {
732
769
// Clang's CodeGen is designed to work with a single llvm::Module. In many
733
770
// cases for convenience various CodeGen parts have a reference to the
734
771
// llvm::Module (TheModule or Module) which does not change when a new
@@ -750,8 +787,10 @@ std::unique_ptr<llvm::Module> Interpreter::GenModule() {
750
787
return nullptr ;
751
788
}
752
789
753
- CodeGenerator *Interpreter::getCodeGen () const {
754
- FrontendAction *WrappedAct = Act->getWrapped ();
790
+ CodeGenerator *Interpreter::getCodeGen (IncrementalAction *Action) const {
791
+ if (!Action)
792
+ Action = Act.get ();
793
+ FrontendAction *WrappedAct = Action->getWrapped ();
755
794
if (!WrappedAct->hasIRSupport ())
756
795
return nullptr ;
757
796
return static_cast <CodeGenAction *>(WrappedAct)->getCodeGenerator ();
0 commit comments