Skip to content

Commit a243698

Browse files
Merge branch 'main' into giselgetrounding
2 parents feafaf2 + 38cd903 commit a243698

File tree

117 files changed

+2177
-923
lines changed

Some content is hidden

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

117 files changed

+2177
-923
lines changed

clang/Maintainers.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Clang MLIR generation
5959
| Bruno Cardoso Lopes
6060
| bruno.cardoso\@gmail.com (email), sonicsprawl (Discord), bcardosolopes (GitHub)
6161
62+
| Henrich Lauko
63+
| henrich.lau\@gmail.com (email), henrich.lauko (Discord), xlauko (GitHub)
6264
6365
Analysis & CFG
6466
~~~~~~~~~~~~~~

clang/include/clang/Basic/Attr.td

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,22 @@ def TargetHasDLLImportExport : TargetSpec {
492492
def TargetItaniumCXXABI : TargetSpec {
493493
let CustomCode = [{ Target.getCXXABI().isItaniumFamily() }];
494494
}
495+
495496
def TargetMicrosoftCXXABI : TargetArch<["x86", "x86_64", "arm", "thumb", "aarch64"]> {
496497
let CustomCode = [{ Target.getCXXABI().isMicrosoft() }];
497498
}
499+
500+
// The target follows Microsoft record layout. Usually this happens in two
501+
// cases: 1. the target itself has Microsoft C++ ABI, e.g. x86_64 in MSVC
502+
// environment on Windows 2. an offloading target e.g. amdgcn or nvptx with
503+
// a host target in MSVC environment on Windows.
504+
def TargetMicrosoftRecordLayout : TargetArch<["x86", "x86_64", "arm", "thumb",
505+
"aarch64", "amdgcn", "nvptx",
506+
"nvptx64", "spirv", "spirv32",
507+
"spirv64"]> {
508+
let CustomCode = [{ Target.hasMicrosoftRecordLayout() }];
509+
}
510+
498511
def TargetELF : TargetSpec {
499512
let ObjectFormats = ["ELF"];
500513
}
@@ -1789,7 +1802,7 @@ def Destructor : InheritableAttr {
17891802
let Documentation = [CtorDtorDocs];
17901803
}
17911804

1792-
def EmptyBases : InheritableAttr, TargetSpecificAttr<TargetMicrosoftCXXABI> {
1805+
def EmptyBases : InheritableAttr, TargetSpecificAttr<TargetMicrosoftRecordLayout> {
17931806
let Spellings = [Declspec<"empty_bases">];
17941807
let Subjects = SubjectList<[CXXRecord]>;
17951808
let Documentation = [EmptyBasesDocs];
@@ -2021,7 +2034,7 @@ def Restrict : InheritableAttr {
20212034
let Documentation = [RestrictDocs];
20222035
}
20232036

2024-
def LayoutVersion : InheritableAttr, TargetSpecificAttr<TargetMicrosoftCXXABI> {
2037+
def LayoutVersion : InheritableAttr, TargetSpecificAttr<TargetMicrosoftRecordLayout> {
20252038
let Spellings = [Declspec<"layout_version">];
20262039
let Args = [UnsignedArgument<"Version">];
20272040
let Subjects = SubjectList<[CXXRecord]>;
@@ -2239,7 +2252,7 @@ def NoUniqueAddress : InheritableAttr {
22392252
let Spellings = [CXX11<"", "no_unique_address", 201803>, CXX11<"msvc", "no_unique_address", 201803>];
22402253
let TargetSpecificSpellings = [
22412254
TargetSpecificSpelling<TargetItaniumCXXABI, [CXX11<"", "no_unique_address", 201803>]>,
2242-
TargetSpecificSpelling<TargetMicrosoftCXXABI, [CXX11<"msvc", "no_unique_address", 201803>]>,
2255+
TargetSpecificSpelling<TargetMicrosoftRecordLayout, [CXX11<"msvc", "no_unique_address", 201803>]>,
22432256
];
22442257
let Documentation = [NoUniqueAddressDocs];
22452258
}

clang/include/clang/Basic/BuiltinsSPIRVCL.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ include "clang/Basic/BuiltinsSPIRVBase.td"
1010

1111
def generic_cast_to_ptr_explicit
1212
: SPIRVBuiltin<"void*(void*, int)", [NoThrow, Const, CustomTypeChecking]>;
13+
def global_size : SPIRVBuiltin<"size_t(int)", [NoThrow, Const]>;
14+
def global_offset : SPIRVBuiltin<"size_t(int)", [NoThrow, Const]>;
15+
def subgroup_max_size : SPIRVBuiltin<"uint32_t()", [NoThrow, Const]>;

clang/include/clang/Basic/BuiltinsSPIRVCommon.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88

99
include "clang/Basic/BuiltinsSPIRVBase.td"
1010

11+
def num_workgroups : SPIRVBuiltin<"size_t(int)", [NoThrow, Const]>;
12+
def workgroup_size : SPIRVBuiltin<"size_t(int)", [NoThrow, Const]>;
13+
def workgroup_id : SPIRVBuiltin<"size_t(int)", [NoThrow, Const]>;
14+
def local_invocation_id : SPIRVBuiltin<"size_t(int)", [NoThrow, Const]>;
15+
def global_invocation_id : SPIRVBuiltin<"size_t(int)", [NoThrow, Const]>;
16+
def subgroup_size : SPIRVBuiltin<"uint32_t()", [NoThrow, Const]>;
17+
def num_subgroups : SPIRVBuiltin<"uint32_t()", [NoThrow, Const]>;
18+
def subgroup_id : SPIRVBuiltin<"uint32_t()", [NoThrow, Const]>;
19+
def subgroup_local_invocation_id : SPIRVBuiltin<"uint32_t()", [NoThrow, Const]>;
20+
1121
def distance : SPIRVBuiltin<"void(...)", [NoThrow, Const]>;
1222
def length : SPIRVBuiltin<"void(...)", [NoThrow, Const]>;
1323
def smoothstep : SPIRVBuiltin<"void(...)", [NoThrow, Const, CustomTypeChecking]>;

clang/include/clang/Basic/TargetInfo.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ class TargetInfo : public TransferrableTargetInfo,
289289

290290
std::optional<llvm::Triple> DarwinTargetVariantTriple;
291291

292+
bool HasMicrosoftRecordLayout = false;
293+
292294
// TargetInfo Constructor. Default initializes all fields.
293295
TargetInfo(const llvm::Triple &T);
294296

@@ -1331,7 +1333,8 @@ class TargetInfo : public TransferrableTargetInfo,
13311333
/// Apply changes to the target information with respect to certain
13321334
/// language options which change the target configuration and adjust
13331335
/// the language based on the target options where applicable.
1334-
virtual void adjust(DiagnosticsEngine &Diags, LangOptions &Opts);
1336+
virtual void adjust(DiagnosticsEngine &Diags, LangOptions &Opts,
1337+
const TargetInfo *Aux);
13351338

13361339
/// Initialize the map with the default set of target features for the
13371340
/// CPU this should include all legal feature strings on the target.
@@ -1846,6 +1849,8 @@ class TargetInfo : public TransferrableTargetInfo,
18461849

18471850
virtual void setAuxTarget(const TargetInfo *Aux) {}
18481851

1852+
bool hasMicrosoftRecordLayout() const { return HasMicrosoftRecordLayout; }
1853+
18491854
/// Whether target allows debuginfo types for decl only variables/functions.
18501855
virtual bool allowDebugInfoForExternalRef() const { return false; }
18511856

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4912,6 +4912,18 @@ bool Compiler<Emitter>::VisitBuiltinCallExpr(const CallExpr *E,
49124912
return true;
49134913
}
49144914

4915+
static const Expr *stripDerivedToBaseCasts(const Expr *E) {
4916+
if (const auto *PE = dyn_cast<ParenExpr>(E))
4917+
return stripDerivedToBaseCasts(PE->getSubExpr());
4918+
4919+
if (const auto *CE = dyn_cast<CastExpr>(E);
4920+
CE &&
4921+
(CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp))
4922+
return stripDerivedToBaseCasts(CE->getSubExpr());
4923+
4924+
return E;
4925+
}
4926+
49154927
template <class Emitter>
49164928
bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
49174929
const FunctionDecl *FuncDecl = E->getDirectCallee();
@@ -4995,6 +5007,7 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
49955007
}
49965008
}
49975009

5010+
bool Devirtualized = false;
49985011
std::optional<unsigned> CalleeOffset;
49995012
// Add the (optional, implicit) This pointer.
50005013
if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
@@ -5013,8 +5026,26 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
50135026
return false;
50145027
if (!this->emitGetMemberPtrBase(E))
50155028
return false;
5016-
} else if (!this->visit(MC->getImplicitObjectArgument())) {
5017-
return false;
5029+
} else {
5030+
const auto *InstancePtr = MC->getImplicitObjectArgument();
5031+
if (isa_and_nonnull<CXXDestructorDecl>(CompilingFunction) ||
5032+
isa_and_nonnull<CXXConstructorDecl>(CompilingFunction)) {
5033+
const auto *Stripped = stripDerivedToBaseCasts(InstancePtr);
5034+
if (isa<CXXThisExpr>(Stripped)) {
5035+
FuncDecl =
5036+
cast<CXXMethodDecl>(FuncDecl)->getCorrespondingMethodInClass(
5037+
Stripped->getType()->getPointeeType()->getAsCXXRecordDecl());
5038+
Devirtualized = true;
5039+
if (!this->visit(Stripped))
5040+
return false;
5041+
} else {
5042+
if (!this->visit(InstancePtr))
5043+
return false;
5044+
}
5045+
} else {
5046+
if (!this->visit(InstancePtr))
5047+
return false;
5048+
}
50185049
}
50195050
} else if (const auto *PD =
50205051
dyn_cast<CXXPseudoDestructorExpr>(E->getCallee())) {
@@ -5060,7 +5091,7 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
50605091

50615092
bool IsVirtual = false;
50625093
if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
5063-
IsVirtual = MD->isVirtual();
5094+
IsVirtual = !Devirtualized && MD->isVirtual();
50645095

50655096
// In any case call the function. The return value will end up on the stack
50665097
// and if the function has RVO, we already have the pointer on the stack to
@@ -6027,6 +6058,8 @@ bool Compiler<Emitter>::visitFunc(const FunctionDecl *F) {
60276058
// Classify the return type.
60286059
ReturnType = this->classify(F->getReturnType());
60296060

6061+
this->CompilingFunction = F;
6062+
60306063
if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F))
60316064
return this->compileConstructor(Ctor);
60326065
if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(F))

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
448448
OptLabelTy ContinueLabel;
449449
/// Default case label.
450450
OptLabelTy DefaultLabel;
451+
452+
const FunctionDecl *CompilingFunction = nullptr;
451453
};
452454

453455
extern template class Compiler<ByteCodeEmitter>;

clang/lib/AST/RecordLayoutBuilder.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,15 +2458,6 @@ static bool mustSkipTailPadding(TargetCXXABI ABI, const CXXRecordDecl *RD) {
24582458
llvm_unreachable("bad tail-padding use kind");
24592459
}
24602460

2461-
static bool isMsLayout(const ASTContext &Context) {
2462-
// Check if it's CUDA device compilation; ensure layout consistency with host.
2463-
if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice &&
2464-
Context.getAuxTargetInfo())
2465-
return Context.getAuxTargetInfo()->getCXXABI().isMicrosoft();
2466-
2467-
return Context.getTargetInfo().getCXXABI().isMicrosoft();
2468-
}
2469-
24702461
// This section contains an implementation of struct layout that is, up to the
24712462
// included tests, compatible with cl.exe (2013). The layout produced is
24722463
// significantly different than those produced by the Itanium ABI. Here we note
@@ -3399,7 +3390,7 @@ ASTContext::getASTRecordLayout(const RecordDecl *D) const {
33993390

34003391
const ASTRecordLayout *NewEntry = nullptr;
34013392

3402-
if (isMsLayout(*this)) {
3393+
if (getTargetInfo().hasMicrosoftRecordLayout()) {
34033394
if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) {
34043395
EmptySubobjectMap EmptySubobjects(*this, RD);
34053396
MicrosoftRecordLayoutBuilder Builder(*this, &EmptySubobjects);
@@ -3656,7 +3647,8 @@ static void DumpRecordLayout(raw_ostream &OS, const RecordDecl *RD,
36563647
bool HasOwnVBPtr = Layout.hasOwnVBPtr();
36573648

36583649
// Vtable pointer.
3659-
if (CXXRD->isDynamicClass() && !PrimaryBase && !isMsLayout(C)) {
3650+
if (CXXRD->isDynamicClass() && !PrimaryBase &&
3651+
!C.getTargetInfo().hasMicrosoftRecordLayout()) {
36603652
PrintOffset(OS, Offset, IndentLevel);
36613653
OS << '(' << *RD << " vtable pointer)\n";
36623654
} else if (HasOwnVFPtr) {
@@ -3754,7 +3746,7 @@ static void DumpRecordLayout(raw_ostream &OS, const RecordDecl *RD,
37543746

37553747
PrintIndentNoOffset(OS, IndentLevel - 1);
37563748
OS << "[sizeof=" << Layout.getSize().getQuantity();
3757-
if (CXXRD && !isMsLayout(C))
3749+
if (CXXRD && !C.getTargetInfo().hasMicrosoftRecordLayout())
37583750
OS << ", dsize=" << Layout.getDataSize().getQuantity();
37593751
OS << ", align=" << Layout.getAlignment().getQuantity();
37603752
if (C.getTargetInfo().defaultsToAIXPowerAlignment())
@@ -3793,7 +3785,7 @@ void ASTContext::DumpRecordLayout(const RecordDecl *RD, raw_ostream &OS,
37933785
OS << "\nLayout: ";
37943786
OS << "<ASTRecordLayout\n";
37953787
OS << " Size:" << toBits(Info.getSize()) << "\n";
3796-
if (!isMsLayout(*this))
3788+
if (!getTargetInfo().hasMicrosoftRecordLayout())
37973789
OS << " DataSize:" << toBits(Info.getDataSize()) << "\n";
37983790
OS << " Alignment:" << toBits(Info.getAlignment()) << "\n";
37993791
if (Target->defaultsToAIXPowerAlignment())

clang/lib/AST/TypePrinter.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,17 @@ void TypePrinter::printAttributedBefore(const AttributedType *T,
18611861
if (T->getAttrKind() == attr::ObjCKindOf)
18621862
OS << "__kindof ";
18631863

1864+
if (T->getAttrKind() == attr::PreserveNone) {
1865+
OS << "__attribute__((preserve_none)) ";
1866+
spaceBeforePlaceHolder(OS);
1867+
} else if (T->getAttrKind() == attr::PreserveMost) {
1868+
OS << "__attribute__((preserve_most)) ";
1869+
spaceBeforePlaceHolder(OS);
1870+
} else if (T->getAttrKind() == attr::PreserveAll) {
1871+
OS << "__attribute__((preserve_all)) ";
1872+
spaceBeforePlaceHolder(OS);
1873+
}
1874+
18641875
if (T->getAttrKind() == attr::AddressSpace)
18651876
printBefore(T->getEquivalentType(), OS);
18661877
else
@@ -1972,6 +1983,13 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
19721983
return;
19731984
}
19741985

1986+
if (T->getAttrKind() == attr::PreserveAll ||
1987+
T->getAttrKind() == attr::PreserveMost ||
1988+
T->getAttrKind() == attr::PreserveNone) {
1989+
// This has to be printed before the type.
1990+
return;
1991+
}
1992+
19751993
OS << " __attribute__((";
19761994
switch (T->getAttrKind()) {
19771995
#define TYPE_ATTR(NAME)
@@ -2036,6 +2054,9 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
20362054
case attr::Blocking:
20372055
case attr::Allocating:
20382056
case attr::SwiftAttr:
2057+
case attr::PreserveAll:
2058+
case attr::PreserveMost:
2059+
case attr::PreserveNone:
20392060
llvm_unreachable("This attribute should have been handled already");
20402061

20412062
case attr::NSReturnsRetained:
@@ -2071,20 +2092,12 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
20712092
case attr::DeviceKernel:
20722093
OS << T->getAttr()->getSpelling();
20732094
break;
2074-
case attr::IntelOclBicc: OS << "inteloclbicc"; break;
2075-
case attr::PreserveMost:
2076-
OS << "preserve_most";
2077-
break;
2078-
2079-
case attr::PreserveAll:
2080-
OS << "preserve_all";
2095+
case attr::IntelOclBicc:
2096+
OS << "inteloclbicc";
20812097
break;
20822098
case attr::M68kRTD:
20832099
OS << "m68k_rtd";
20842100
break;
2085-
case attr::PreserveNone:
2086-
OS << "preserve_none";
2087-
break;
20882101
case attr::RISCVVectorCC:
20892102
OS << "riscv_vector_cc";
20902103
break;

clang/lib/Basic/TargetInfo.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) {
176176
? TargetCXXABI::Microsoft
177177
: TargetCXXABI::GenericItanium);
178178

179+
HasMicrosoftRecordLayout = TheCXXABI.isMicrosoft();
180+
179181
// Default to an empty address space map.
180182
AddrSpaceMap = &DefaultAddrSpaceMap;
181183
UseAddrSpaceMapMangling = false;
@@ -410,7 +412,8 @@ bool TargetInfo::isTypeSigned(IntType T) {
410412
/// Apply changes to the target information with respect to certain
411413
/// language options which change the target configuration and adjust
412414
/// the language based on the target options where applicable.
413-
void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
415+
void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts,
416+
const TargetInfo *Aux) {
414417
if (Opts.NoBitFieldTypeAlign)
415418
UseBitFieldTypeAlignment = false;
416419

@@ -550,6 +553,10 @@ void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
550553

551554
if (Opts.FakeAddressSpaceMap)
552555
AddrSpaceMap = &FakeAddrSpaceMap;
556+
557+
// Check if it's CUDA device compilation; ensure layout consistency with host.
558+
if (Opts.CUDA && Opts.CUDAIsDevice && Aux && !HasMicrosoftRecordLayout)
559+
HasMicrosoftRecordLayout = Aux->getCXXABI().isMicrosoft();
553560
}
554561

555562
bool TargetInfo::initFeatureMap(

0 commit comments

Comments
 (0)