Skip to content

Commit 3159486

Browse files
Merge branch 'release/20.x' into revert_121559_release20
2 parents 026cebe + ebfae55 commit 3159486

File tree

41 files changed

+937
-182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+937
-182
lines changed

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CXXRecordDecl;
4141
class Decl;
4242
class IncrementalExecutor;
4343
class IncrementalParser;
44+
class IncrementalCUDADeviceParser;
4445

4546
/// Create a pre-configured \c CompilerInstance for incremental processing.
4647
class IncrementalCompilerBuilder {
@@ -93,7 +94,10 @@ class Interpreter {
9394
std::unique_ptr<IncrementalExecutor> IncrExecutor;
9495

9596
// An optional parser for CUDA offloading
96-
std::unique_ptr<IncrementalParser> DeviceParser;
97+
std::unique_ptr<IncrementalCUDADeviceParser> DeviceParser;
98+
99+
// An optional action for CUDA offloading
100+
std::unique_ptr<IncrementalAction> DeviceAct;
97101

98102
/// List containing information about each incrementally parsed piece of code.
99103
std::list<PartialTranslationUnit> PTUs;
@@ -175,10 +179,11 @@ class Interpreter {
175179
llvm::Expected<Expr *> ExtractValueFromExpr(Expr *E);
176180
llvm::Expected<llvm::orc::ExecutorAddr> CompileDtorCall(CXXRecordDecl *CXXRD);
177181

178-
CodeGenerator *getCodeGen() const;
179-
std::unique_ptr<llvm::Module> GenModule();
182+
CodeGenerator *getCodeGen(IncrementalAction *Action = nullptr) const;
183+
std::unique_ptr<llvm::Module> GenModule(IncrementalAction *Action = nullptr);
180184
PartialTranslationUnit &RegisterPTU(TranslationUnitDecl *TU,
181-
std::unique_ptr<llvm::Module> M = {});
185+
std::unique_ptr<llvm::Module> M = {},
186+
IncrementalAction *Action = nullptr);
182187

183188
// A cache for the compiled destructors used to for de-allocation of managed
184189
// clang::Values.

clang/lib/AST/Expr.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,8 +1665,11 @@ SourceLocation CallExpr::getBeginLoc() const {
16651665
Method && Method->isExplicitObjectMemberFunction()) {
16661666
bool HasFirstArg = getNumArgs() > 0 && getArg(0);
16671667
assert(HasFirstArg);
1668-
if (HasFirstArg)
1669-
return getArg(0)->getBeginLoc();
1668+
if (HasFirstArg) {
1669+
if (auto FirstArgLoc = getArg(0)->getBeginLoc(); FirstArgLoc.isValid()) {
1670+
return FirstArgLoc;
1671+
}
1672+
}
16701673
}
16711674
}
16721675

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,13 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
349349
}
350350
}
351351

352+
// Allow breaking before the right parens with block indentation if there was
353+
// a break after the left parens, which is tracked by BreakBeforeClosingParen.
354+
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
355+
Current.is(tok::r_paren)) {
356+
return CurrentState.BreakBeforeClosingParen;
357+
}
358+
352359
// Don't allow breaking before a closing brace of a block-indented braced list
353360
// initializer if there isn't already a break.
354361
if (Current.is(tok::r_brace) && Current.MatchingParen &&

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3961,8 +3961,10 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
39613961
FormatToken *AfterLastAttribute = nullptr;
39623962
FormatToken *ClosingParen = nullptr;
39633963

3964-
for (auto *Tok = FirstNonComment ? FirstNonComment->Next : nullptr; Tok;
3965-
Tok = Tok->Next) {
3964+
for (auto *Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using)
3965+
? FirstNonComment->Next
3966+
: nullptr;
3967+
Tok; Tok = Tok->Next) {
39663968
if (Tok->is(TT_StartOfName))
39673969
SeenName = true;
39683970
if (Tok->Previous->EndsCppAttributeGroup)

clang/lib/Interpreter/DeviceOffload.cpp

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,17 @@ IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
3131
llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs)
3232
: IncrementalParser(*DeviceInstance, Err), PTUs(PTUs), VFS(FS),
3333
CodeGenOpts(HostInstance.getCodeGenOpts()),
34-
TargetOpts(HostInstance.getTargetOpts()) {
34+
TargetOpts(DeviceInstance->getTargetOpts()) {
3535
if (Err)
3636
return;
37-
DeviceCI = std::move(DeviceInstance);
3837
StringRef Arch = TargetOpts.CPU;
3938
if (!Arch.starts_with("sm_") || Arch.substr(3).getAsInteger(10, SMVersion)) {
4039
Err = llvm::joinErrors(std::move(Err), llvm::make_error<llvm::StringError>(
4140
"Invalid CUDA architecture",
4241
llvm::inconvertibleErrorCode()));
4342
return;
4443
}
45-
}
46-
47-
llvm::Expected<TranslationUnitDecl *>
48-
IncrementalCUDADeviceParser::Parse(llvm::StringRef Input) {
49-
auto PTU = IncrementalParser::Parse(Input);
50-
if (!PTU)
51-
return PTU.takeError();
52-
53-
auto PTX = GeneratePTX();
54-
if (!PTX)
55-
return PTX.takeError();
56-
57-
auto Err = GenerateFatbinary();
58-
if (Err)
59-
return std::move(Err);
60-
61-
std::string FatbinFileName =
62-
"/incr_module_" + std::to_string(PTUs.size()) + ".fatbin";
63-
VFS->addFile(FatbinFileName, 0,
64-
llvm::MemoryBuffer::getMemBuffer(
65-
llvm::StringRef(FatbinContent.data(), FatbinContent.size()),
66-
"", false));
67-
68-
CodeGenOpts.CudaGpuBinaryFileName = FatbinFileName;
69-
70-
FatbinContent.clear();
71-
72-
return PTU;
44+
DeviceCI = std::move(DeviceInstance);
7345
}
7446

7547
llvm::Expected<llvm::StringRef> IncrementalCUDADeviceParser::GeneratePTX() {
@@ -172,6 +144,19 @@ llvm::Error IncrementalCUDADeviceParser::GenerateFatbinary() {
172144

173145
FatbinContent.append(PTXCode.begin(), PTXCode.end());
174146

147+
const PartialTranslationUnit &PTU = PTUs.back();
148+
149+
std::string FatbinFileName = "/" + PTU.TheModule->getName().str() + ".fatbin";
150+
151+
VFS->addFile(FatbinFileName, 0,
152+
llvm::MemoryBuffer::getMemBuffer(
153+
llvm::StringRef(FatbinContent.data(), FatbinContent.size()),
154+
"", false));
155+
156+
CodeGenOpts.CudaGpuBinaryFileName = FatbinFileName;
157+
158+
FatbinContent.clear();
159+
175160
return llvm::Error::success();
176161
}
177162

clang/lib/Interpreter/DeviceOffload.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class IncrementalCUDADeviceParser : public IncrementalParser {
3333
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
3434
llvm::Error &Err, const std::list<PartialTranslationUnit> &PTUs);
3535

36-
llvm::Expected<TranslationUnitDecl *> Parse(llvm::StringRef Input) override;
37-
3836
// Generate PTX for the last PTU.
3937
llvm::Expected<llvm::StringRef> GeneratePTX();
4038

clang/lib/Interpreter/IncrementalExecutor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class IncrementalExecutor {
5757
virtual llvm::Error removeModule(PartialTranslationUnit &PTU);
5858
virtual llvm::Error runCtors() const;
5959
virtual llvm::Error cleanUp();
60-
llvm::Expected<llvm::orc::ExecutorAddr>
60+
virtual llvm::Expected<llvm::orc::ExecutorAddr>
6161
getSymbolAddress(llvm::StringRef Name, SymbolNameKind NameKind) const;
6262

6363
llvm::orc::LLJIT &GetExecutionEngine() { return *Jit; }

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Support/VirtualFileSystem.h"
1919
#ifdef __EMSCRIPTEN__
2020
#include "Wasm.h"
21+
#include <dlfcn.h>
2122
#endif // __EMSCRIPTEN__
2223

2324
#include "clang/AST/ASTConsumer.h"
@@ -480,20 +481,34 @@ Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI,
480481
OverlayVFS->pushOverlay(IMVFS);
481482
CI->createFileManager(OverlayVFS);
482483

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);
486490

487491
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+
491497
if (Err)
492498
return std::move(Err);
493499

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);
495509

496-
return Interp;
510+
Interp->DeviceParser = std::move(DeviceParser);
511+
return std::move(Interp);
497512
}
498513

499514
const CompilerInstance *Interpreter::getCompilerInstance() const {
@@ -531,15 +546,17 @@ size_t Interpreter::getEffectivePTUSize() const {
531546

532547
PartialTranslationUnit &
533548
Interpreter::RegisterPTU(TranslationUnitDecl *TU,
534-
std::unique_ptr<llvm::Module> M /*={}*/) {
549+
std::unique_ptr<llvm::Module> M /*={}*/,
550+
IncrementalAction *Action) {
535551
PTUs.emplace_back(PartialTranslationUnit());
536552
PartialTranslationUnit &LastPTU = PTUs.back();
537553
LastPTU.TUPart = TU;
538554

539555
if (!M)
540-
M = GenModule();
556+
M = GenModule(Action);
541557

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");
543560

544561
LastPTU.TheModule = std::move(M);
545562
LLVM_DEBUG(llvm::dbgs() << "compile-ptu " << PTUs.size() - 1
@@ -559,6 +576,16 @@ Interpreter::Parse(llvm::StringRef Code) {
559576
llvm::Expected<TranslationUnitDecl *> DeviceTU = DeviceParser->Parse(Code);
560577
if (auto E = DeviceTU.takeError())
561578
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);
562589
}
563590

564591
// Tell the interpreter sliently ignore unused expressions since value
@@ -711,6 +738,14 @@ llvm::Error Interpreter::Undo(unsigned N) {
711738
}
712739

713740
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
714749
auto EE = getExecutionEngine();
715750
if (!EE)
716751
return EE.takeError();
@@ -722,13 +757,15 @@ llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
722757
EE->getMainJITDylib().addGenerator(std::move(*DLSG));
723758
else
724759
return DLSG.takeError();
760+
#endif
725761

726762
return llvm::Error::success();
727763
}
728764

729-
std::unique_ptr<llvm::Module> Interpreter::GenModule() {
765+
std::unique_ptr<llvm::Module>
766+
Interpreter::GenModule(IncrementalAction *Action) {
730767
static unsigned ID = 0;
731-
if (CodeGenerator *CG = getCodeGen()) {
768+
if (CodeGenerator *CG = getCodeGen(Action)) {
732769
// Clang's CodeGen is designed to work with a single llvm::Module. In many
733770
// cases for convenience various CodeGen parts have a reference to the
734771
// llvm::Module (TheModule or Module) which does not change when a new
@@ -750,8 +787,10 @@ std::unique_ptr<llvm::Module> Interpreter::GenModule() {
750787
return nullptr;
751788
}
752789

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();
755794
if (!WrappedAct->hasIRSupport())
756795
return nullptr;
757796
return static_cast<CodeGenAction *>(WrappedAct)->getCodeGenerator();

clang/lib/Interpreter/Wasm.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ llvm::Error WasmIncrementalExecutor::cleanUp() {
144144
return llvm::Error::success();
145145
}
146146

147+
llvm::Expected<llvm::orc::ExecutorAddr>
148+
WasmIncrementalExecutor::getSymbolAddress(llvm::StringRef Name,
149+
SymbolNameKind NameKind) const {
150+
void *Sym = dlsym(RTLD_DEFAULT, Name.str().c_str());
151+
if (!Sym) {
152+
return llvm::make_error<llvm::StringError>("dlsym failed for symbol: " +
153+
Name.str(),
154+
llvm::inconvertibleErrorCode());
155+
}
156+
157+
return llvm::orc::ExecutorAddr::fromPtr(Sym);
158+
}
159+
147160
WasmIncrementalExecutor::~WasmIncrementalExecutor() = default;
148161

149162
} // namespace clang

clang/lib/Interpreter/Wasm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class WasmIncrementalExecutor : public IncrementalExecutor {
2929
llvm::Error removeModule(PartialTranslationUnit &PTU) override;
3030
llvm::Error runCtors() const override;
3131
llvm::Error cleanUp() override;
32+
llvm::Expected<llvm::orc::ExecutorAddr>
33+
getSymbolAddress(llvm::StringRef Name,
34+
SymbolNameKind NameKind) const override;
3235

3336
~WasmIncrementalExecutor() override;
3437
};

0 commit comments

Comments
 (0)