Skip to content

Commit 7202600

Browse files
authored
merge main into amd-staging (llvm#1656)
2 parents 4a704db + 22c6661 commit 7202600

File tree

94 files changed

+1320
-761
lines changed

Some content is hidden

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

94 files changed

+1320
-761
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7040,6 +7040,13 @@ let Flags = [TargetSpecific] in {
70407040
defm android_pad_segment : BooleanFFlag<"android-pad-segment">, Group<f_Group>;
70417041
} // let Flags = [TargetSpecific]
70427042

7043+
def shared_libflangrt : Flag<["-"], "shared-libflangrt">,
7044+
HelpText<"Link the flang-rt shared library">, Group<Link_Group>,
7045+
Visibility<[FlangOption]>, Flags<[NoArgumentUnused]>;
7046+
def static_libflangrt : Flag<["-"], "static-libflangrt">,
7047+
HelpText<"Link the flang-rt static library">, Group<Link_Group>,
7048+
Visibility<[FlangOption]>, Flags<[NoArgumentUnused]>;
7049+
70437050
//===----------------------------------------------------------------------===//
70447051
// FLangOption + NoXarchOption
70457052
//===----------------------------------------------------------------------===//

clang/include/clang/Driver/ToolChain.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,10 @@ class ToolChain {
526526
addFortranRuntimeLibraryPath(const llvm::opt::ArgList &Args,
527527
llvm::opt::ArgStringList &CmdArgs) const;
528528

529+
/// Add the path for libflang_rt.runtime.a
530+
void addFlangRTLibPath(const llvm::opt::ArgList &Args,
531+
llvm::opt::ArgStringList &CmdArgs) const;
532+
529533
const char *getCompilerRTArgString(const llvm::opt::ArgList &Args,
530534
StringRef Component,
531535
FileType Type = ToolChain::FT_Static,

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5431,39 +5431,39 @@ bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
54315431
this->fallthrough(CondLabel);
54325432
this->emitLabel(CondLabel);
54335433

5434-
{
5435-
LocalScope<Emitter> CondScope(this);
5436-
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5437-
if (!visitDeclStmt(CondDecl))
5438-
return false;
5439-
5440-
if (Cond) {
5441-
if (!this->visitBool(Cond))
5442-
return false;
5443-
if (!this->jumpFalse(EndLabel))
5444-
return false;
5445-
}
5446-
5447-
if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
5448-
return false;
5449-
5450-
if (Body && !this->visitStmt(Body))
5434+
// Start of loop body.
5435+
LocalScope<Emitter> CondScope(this);
5436+
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5437+
if (!visitDeclStmt(CondDecl))
54515438
return false;
54525439

5453-
this->fallthrough(IncLabel);
5454-
this->emitLabel(IncLabel);
5455-
if (Inc && !this->discard(Inc))
5440+
if (Cond) {
5441+
if (!this->visitBool(Cond))
54565442
return false;
5457-
5458-
if (!CondScope.destroyLocals())
5443+
if (!this->jumpFalse(EndLabel))
54595444
return false;
54605445
}
5446+
if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
5447+
return false;
5448+
5449+
if (Body && !this->visitStmt(Body))
5450+
return false;
5451+
5452+
this->fallthrough(IncLabel);
5453+
this->emitLabel(IncLabel);
5454+
if (Inc && !this->discard(Inc))
5455+
return false;
5456+
5457+
if (!CondScope.destroyLocals())
5458+
return false;
54615459
if (!this->jump(CondLabel))
54625460
return false;
5461+
// End of loop body.
54635462

5464-
this->fallthrough(EndLabel);
54655463
this->emitLabel(EndLabel);
5466-
return true;
5464+
// If we jumped out of the loop above, we still need to clean up the condition
5465+
// scope.
5466+
return CondScope.destroyLocals();
54675467
}
54685468

54695469
template <class Emitter>

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,10 @@ template <class Emitter> class LocalScope : public VariableScope<Emitter> {
531531
if (!Idx)
532532
return true;
533533

534+
// NB: We are *not* resetting Idx here as to allow multiple
535+
// calls to destroyLocals().
534536
bool Success = this->emitDestructors(E);
535537
this->Ctx->emitDestroy(*Idx, E);
536-
this->Idx = std::nullopt;
537538
return Success;
538539
}
539540

clang/lib/AST/ByteCode/Disasm.cpp

Lines changed: 152 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,74 @@
3333
using namespace clang;
3434
using namespace clang::interp;
3535

36-
template <typename T> inline static T ReadArg(Program &P, CodePtr &OpPC) {
36+
template <typename T>
37+
inline static std::string printArg(Program &P, CodePtr &OpPC) {
3738
if constexpr (std::is_pointer_v<T>) {
3839
uint32_t ID = OpPC.read<uint32_t>();
39-
return reinterpret_cast<T>(P.getNativePointer(ID));
40+
std::string Result;
41+
llvm::raw_string_ostream SS(Result);
42+
SS << reinterpret_cast<T>(P.getNativePointer(ID));
43+
return Result;
4044
} else {
41-
return OpPC.read<T>();
45+
std::string Result;
46+
llvm::raw_string_ostream SS(Result);
47+
auto Arg = OpPC.read<T>();
48+
SS << Arg;
49+
return Result;
4250
}
4351
}
4452

45-
template <> inline Floating ReadArg<Floating>(Program &P, CodePtr &OpPC) {
46-
Floating F = Floating::deserialize(*OpPC);
53+
template <> inline std::string printArg<Floating>(Program &P, CodePtr &OpPC) {
54+
auto F = Floating::deserialize(*OpPC);
4755
OpPC += align(F.bytesToSerialize());
48-
return F;
56+
57+
std::string Result;
58+
llvm::raw_string_ostream SS(Result);
59+
SS << F;
60+
return Result;
4961
}
5062

5163
template <>
52-
inline IntegralAP<false> ReadArg<IntegralAP<false>>(Program &P, CodePtr &OpPC) {
53-
IntegralAP<false> I = IntegralAP<false>::deserialize(*OpPC);
54-
OpPC += align(I.bytesToSerialize());
55-
return I;
56-
}
64+
inline std::string printArg<IntegralAP<false>>(Program &P, CodePtr &OpPC) {
65+
auto F = IntegralAP<false>::deserialize(*OpPC);
66+
OpPC += align(F.bytesToSerialize());
5767

68+
std::string Result;
69+
llvm::raw_string_ostream SS(Result);
70+
SS << F;
71+
return Result;
72+
}
5873
template <>
59-
inline IntegralAP<true> ReadArg<IntegralAP<true>>(Program &P, CodePtr &OpPC) {
60-
IntegralAP<true> I = IntegralAP<true>::deserialize(*OpPC);
61-
OpPC += align(I.bytesToSerialize());
62-
return I;
74+
inline std::string printArg<IntegralAP<true>>(Program &P, CodePtr &OpPC) {
75+
auto F = IntegralAP<true>::deserialize(*OpPC);
76+
OpPC += align(F.bytesToSerialize());
77+
78+
std::string Result;
79+
llvm::raw_string_ostream SS(Result);
80+
SS << F;
81+
return Result;
6382
}
6483

65-
template <> inline FixedPoint ReadArg<FixedPoint>(Program &P, CodePtr &OpPC) {
66-
FixedPoint I = FixedPoint::deserialize(*OpPC);
67-
OpPC += align(I.bytesToSerialize());
68-
return I;
84+
template <> inline std::string printArg<FixedPoint>(Program &P, CodePtr &OpPC) {
85+
auto F = FixedPoint::deserialize(*OpPC);
86+
OpPC += align(F.bytesToSerialize());
87+
88+
std::string Result;
89+
llvm::raw_string_ostream SS(Result);
90+
SS << F;
91+
return Result;
92+
}
93+
94+
static bool isJumpOpcode(Opcode Op) {
95+
return Op == OP_Jmp || Op == OP_Jf || Op == OP_Jt;
96+
}
97+
98+
static size_t getNumDisplayWidth(size_t N) {
99+
unsigned L = 1u, M = 10u;
100+
while (M <= N && ++L != std::numeric_limits<size_t>::digits10 + 1)
101+
M *= 10u;
102+
103+
return L;
69104
}
70105

71106
LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
@@ -80,23 +115,115 @@ LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
80115
OS << "rvo: " << hasRVO() << "\n";
81116
OS << "this arg: " << hasThisPointer() << "\n";
82117

83-
auto PrintName = [&OS](const char *Name) {
84-
OS << Name;
85-
long N = 30 - strlen(Name);
86-
if (N > 0)
87-
OS.indent(N);
118+
struct OpText {
119+
size_t Addr;
120+
std::string Op;
121+
bool IsJump;
122+
llvm::SmallVector<std::string> Args;
88123
};
89124

125+
auto PrintName = [](const char *Name) -> std::string {
126+
return std::string(Name);
127+
};
128+
129+
llvm::SmallVector<OpText> Code;
130+
size_t LongestAddr = 0;
131+
size_t LongestOp = 0;
132+
90133
for (CodePtr Start = getCodeBegin(), PC = Start; PC != getCodeEnd();) {
91134
size_t Addr = PC - Start;
135+
OpText Text;
92136
auto Op = PC.read<Opcode>();
93-
OS << llvm::format("%8d", Addr) << " ";
137+
Text.Addr = Addr;
138+
Text.IsJump = isJumpOpcode(Op);
94139
switch (Op) {
95140
#define GET_DISASM
96141
#include "Opcodes.inc"
97142
#undef GET_DISASM
98143
}
144+
Code.push_back(Text);
145+
LongestOp = std::max(Text.Op.size(), LongestOp);
146+
LongestAddr = std::max(getNumDisplayWidth(Addr), LongestAddr);
99147
}
148+
149+
// Record jumps and their targets.
150+
struct JmpData {
151+
size_t From;
152+
size_t To;
153+
};
154+
llvm::SmallVector<JmpData> Jumps;
155+
for (auto &Text : Code) {
156+
if (Text.IsJump)
157+
Jumps.push_back({Text.Addr, Text.Addr + std::stoi(Text.Args[0]) +
158+
align(sizeof(Opcode)) +
159+
align(sizeof(int32_t))});
160+
}
161+
162+
llvm::SmallVector<std::string> Text;
163+
Text.reserve(Code.size());
164+
size_t LongestLine = 0;
165+
// Print code to a string, one at a time.
166+
for (auto C : Code) {
167+
std::string Line;
168+
llvm::raw_string_ostream LS(Line);
169+
LS << C.Addr;
170+
LS.indent(LongestAddr - getNumDisplayWidth(C.Addr) + 4);
171+
LS << C.Op;
172+
LS.indent(LongestOp - C.Op.size() + 4);
173+
for (auto &Arg : C.Args) {
174+
LS << Arg << ' ';
175+
}
176+
Text.push_back(Line);
177+
LongestLine = std::max(Line.size(), LongestLine);
178+
}
179+
180+
assert(Code.size() == Text.size());
181+
182+
auto spaces = [](unsigned N) -> std::string {
183+
std::string S;
184+
for (unsigned I = 0; I != N; ++I)
185+
S += ' ';
186+
return S;
187+
};
188+
189+
// Now, draw the jump lines.
190+
for (auto &J : Jumps) {
191+
if (J.To > J.From) {
192+
bool FoundStart = false;
193+
for (size_t LineIndex = 0; LineIndex != Text.size(); ++LineIndex) {
194+
Text[LineIndex] += spaces(LongestLine - Text[LineIndex].size());
195+
196+
if (Code[LineIndex].Addr == J.From) {
197+
Text[LineIndex] += " --+";
198+
FoundStart = true;
199+
} else if (Code[LineIndex].Addr == J.To) {
200+
Text[LineIndex] += " <-+";
201+
break;
202+
} else if (FoundStart) {
203+
Text[LineIndex] += " |";
204+
}
205+
}
206+
LongestLine += 5;
207+
} else {
208+
bool FoundStart = false;
209+
for (ssize_t LineIndex = Text.size() - 1; LineIndex >= 0; --LineIndex) {
210+
Text[LineIndex] += spaces(LongestLine - Text[LineIndex].size());
211+
if (Code[LineIndex].Addr == J.From) {
212+
Text[LineIndex] += " --+";
213+
FoundStart = true;
214+
} else if (Code[LineIndex].Addr == J.To) {
215+
Text[LineIndex] += " <-+";
216+
break;
217+
} else if (FoundStart) {
218+
Text[LineIndex] += " |";
219+
}
220+
}
221+
LongestLine += 5;
222+
}
223+
}
224+
225+
for (auto &Line : Text)
226+
OS << Line << '\n';
100227
}
101228

102229
LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); }

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ bool isConstexprUnknown(const Pointer &P) {
307307
if (P.isDummy())
308308
return false;
309309
const VarDecl *VD = P.block()->getDescriptor()->asVarDecl();
310-
return VD && VD->hasLocalStorage();
310+
return VD && VD->hasLocalStorage() && !isa<ParmVarDecl>(VD);
311311
}
312312

313313
bool CheckBCPResult(InterpState &S, const Pointer &Ptr) {

clang/lib/AST/ByteCode/Interp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,11 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
771771
bool CanOverflow) {
772772
assert(!Ptr.isDummy());
773773

774+
if (!S.inConstantContext()) {
775+
if (isConstexprUnknown(Ptr))
776+
return false;
777+
}
778+
774779
if constexpr (std::is_same_v<T, Boolean>) {
775780
if (!S.getLangOpts().CPlusPlus14)
776781
return Invalid(S, OpPC);

clang/lib/Basic/SourceManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/StringRef.h"
2525
#include "llvm/ADT/StringSwitch.h"
2626
#include "llvm/Support/Allocator.h"
27+
#include "llvm/Support/AutoConvert.h"
2728
#include "llvm/Support/Capacity.h"
2829
#include "llvm/Support/Compiler.h"
2930
#include "llvm/Support/Endian.h"

clang/lib/CodeGen/Targets/AMDGPU.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,10 @@ ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
198198
/*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
199199
}
200200

201-
// FIXME: Should also use this for OpenCL, but it requires addressing the
202-
// problem of kernels being called.
203-
//
204201
// FIXME: This doesn't apply the optimization of coercing pointers in structs
205202
// to global address space when using byref. This would require implementing a
206203
// new kind of coercion of the in-memory type when for indirect arguments.
207-
if (!getContext().getLangOpts().OpenCL && LTy == OrigLTy &&
208-
isAggregateTypeForABI(Ty)) {
204+
if (LTy == OrigLTy && isAggregateTypeForABI(Ty)) {
209205
return ABIArgInfo::getIndirectAliased(
210206
getContext().getTypeAlignInChars(Ty),
211207
getContext().getTargetAddressSpace(LangAS::opencl_constant),

0 commit comments

Comments
 (0)