Skip to content

Commit e31494e

Browse files
author
z1_cciauto
authored
merge main into amd-staging (llvm#3536)
2 parents bff9677 + d068eca commit e31494e

File tree

40 files changed

+846
-260
lines changed

40 files changed

+846
-260
lines changed

.ci/utils.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ function at-exit {
2626
mkdir -p artifacts
2727
sccache --show-stats >> artifacts/sccache_stats.txt
2828
cp "${BUILD_DIR}"/.ninja_log artifacts/.ninja_log
29+
cp "${MONOREPO_ROOT}"/*.log artifacts/ || :
2930
cp "${BUILD_DIR}"/test-results.*.xml artifacts/ || :
3031

3132
# If building fails there will be no results files.
3233
shopt -s nullglob
3334

3435
if [[ "$GITHUB_STEP_SUMMARY" != "" ]]; then
3536
python "${MONOREPO_ROOT}"/.ci/generate_test_report_github.py \
36-
$retcode "${BUILD_DIR}"/test-results.*.xml "${BUILD_DIR}"/ninja*.log \
37+
$retcode "${BUILD_DIR}"/test-results.*.xml "${MONOREPO_ROOT}"/ninja*.log \
3738
>> $GITHUB_STEP_SUMMARY
3839
fi
3940
}

clang/include/clang/Basic/Attr.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4932,6 +4932,7 @@ def HLSLResourceBinding: InheritableAttr {
49324932
return SpaceNumber;
49334933
}
49344934
void setImplicitBindingOrderID(uint32_t Value) {
4935+
assert(!hasImplicitBindingOrderID() && "attribute already has implicit binding order id");
49354936
ImplicitBindingOrderID = Value;
49364937
}
49374938
bool hasImplicitBindingOrderID() const {

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,7 +2339,40 @@ llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
23392339
return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
23402340
}
23412341

2342+
// Generalize pointer types to a void pointer with the qualifiers of the
2343+
// originally pointed-to type, e.g. 'const char *' and 'char * const *'
2344+
// generalize to 'const void *' while 'char *' and 'const char **' generalize to
2345+
// 'void *'.
2346+
static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
2347+
if (!Ty->isPointerType())
2348+
return Ty;
2349+
2350+
return Ctx.getPointerType(
2351+
QualType(Ctx.VoidTy)
2352+
.withCVRQualifiers(Ty->getPointeeType().getCVRQualifiers()));
2353+
}
2354+
2355+
// Apply type generalization to a FunctionType's return and argument types
2356+
static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) {
2357+
if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
2358+
SmallVector<QualType, 8> GeneralizedParams;
2359+
for (auto &Param : FnType->param_types())
2360+
GeneralizedParams.push_back(GeneralizeType(Ctx, Param));
2361+
2362+
return Ctx.getFunctionType(GeneralizeType(Ctx, FnType->getReturnType()),
2363+
GeneralizedParams, FnType->getExtProtoInfo());
2364+
}
2365+
2366+
if (auto *FnType = Ty->getAs<FunctionNoProtoType>())
2367+
return Ctx.getFunctionNoProtoType(
2368+
GeneralizeType(Ctx, FnType->getReturnType()));
2369+
2370+
llvm_unreachable("Encountered unknown FunctionType");
2371+
}
2372+
23422373
llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) {
2374+
if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
2375+
T = GeneralizeFunctionType(getContext(), T);
23432376
if (auto *FnType = T->getAs<FunctionProtoType>())
23442377
T = getContext().getFunctionType(
23452378
FnType->getReturnType(), FnType->getParamTypes(),
@@ -2352,6 +2385,8 @@ llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) {
23522385

23532386
if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
23542387
Out << ".normalized";
2388+
if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
2389+
Out << ".generalized";
23552390

23562391
return llvm::ConstantInt::get(Int32Ty,
23572392
static_cast<uint32_t>(llvm::xxHash64(OutName)));
@@ -7887,38 +7922,6 @@ CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
78877922
return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual");
78887923
}
78897924

7890-
// Generalize pointer types to a void pointer with the qualifiers of the
7891-
// originally pointed-to type, e.g. 'const char *' and 'char * const *'
7892-
// generalize to 'const void *' while 'char *' and 'const char **' generalize to
7893-
// 'void *'.
7894-
static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
7895-
if (!Ty->isPointerType())
7896-
return Ty;
7897-
7898-
return Ctx.getPointerType(
7899-
QualType(Ctx.VoidTy).withCVRQualifiers(
7900-
Ty->getPointeeType().getCVRQualifiers()));
7901-
}
7902-
7903-
// Apply type generalization to a FunctionType's return and argument types
7904-
static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) {
7905-
if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
7906-
SmallVector<QualType, 8> GeneralizedParams;
7907-
for (auto &Param : FnType->param_types())
7908-
GeneralizedParams.push_back(GeneralizeType(Ctx, Param));
7909-
7910-
return Ctx.getFunctionType(
7911-
GeneralizeType(Ctx, FnType->getReturnType()),
7912-
GeneralizedParams, FnType->getExtProtoInfo());
7913-
}
7914-
7915-
if (auto *FnType = Ty->getAs<FunctionNoProtoType>())
7916-
return Ctx.getFunctionNoProtoType(
7917-
GeneralizeType(Ctx, FnType->getReturnType()));
7918-
7919-
llvm_unreachable("Encountered unknown FunctionType");
7920-
}
7921-
79227925
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) {
79237926
return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T),
79247927
GeneralizedMetadataIdMap, ".generalized");

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,8 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
851851
}
852852

853853
if (AllAddedKinds & SanitizerKind::KCFI) {
854+
CfiICallGeneralizePointers =
855+
Args.hasArg(options::OPT_fsanitize_cfi_icall_generalize_pointers);
854856
CfiICallNormalizeIntegers =
855857
Args.hasArg(options::OPT_fsanitize_cfi_icall_normalize_integers);
856858

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ static RegisterType getRegisterType(ResourceClass RC) {
7171
llvm_unreachable("unexpected ResourceClass value");
7272
}
7373

74+
static RegisterType getRegisterType(const HLSLAttributedResourceType *ResTy) {
75+
return getRegisterType(ResTy->getAttrs().ResourceClass);
76+
}
77+
7478
// Converts the first letter of string Slot to RegisterType.
7579
// Returns false if the letter does not correspond to a valid register type.
7680
static bool convertToRegisterType(StringRef Slot, RegisterType *RT) {
@@ -342,6 +346,16 @@ static bool isResourceRecordTypeOrArrayOf(VarDecl *VD) {
342346
return Ty->isHLSLResourceRecord() || Ty->isHLSLResourceRecordArray();
343347
}
344348

349+
static const HLSLAttributedResourceType *
350+
getResourceArrayHandleType(VarDecl *VD) {
351+
assert(VD->getType()->isHLSLResourceRecordArray() &&
352+
"expected array of resource records");
353+
const Type *Ty = VD->getType()->getUnqualifiedDesugaredType();
354+
while (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
355+
Ty = CAT->getArrayElementTypeNoTypeQual()->getUnqualifiedDesugaredType();
356+
return HLSLAttributedResourceType::findHandleTypeOnResource(Ty);
357+
}
358+
345359
// Returns true if the type is a leaf element type that is not valid to be
346360
// included in HLSL Buffer, such as a resource class, empty struct, zero-sized
347361
// array, or a builtin intangible type. Returns false it is a valid leaf element
@@ -568,16 +582,13 @@ void createHostLayoutStructForBuffer(Sema &S, HLSLBufferDecl *BufDecl) {
568582
BufDecl->addLayoutStruct(LS);
569583
}
570584

571-
static void addImplicitBindingAttrToBuffer(Sema &S, HLSLBufferDecl *BufDecl,
572-
uint32_t ImplicitBindingOrderID) {
573-
RegisterType RT =
574-
BufDecl->isCBuffer() ? RegisterType::CBuffer : RegisterType::SRV;
585+
static void addImplicitBindingAttrToDecl(Sema &S, Decl *D, RegisterType RT,
586+
uint32_t ImplicitBindingOrderID) {
575587
auto *Attr =
576588
HLSLResourceBindingAttr::CreateImplicit(S.getASTContext(), "", "0", {});
577-
std::optional<unsigned> RegSlot;
578-
Attr->setBinding(RT, RegSlot, 0);
589+
Attr->setBinding(RT, std::nullopt, 0);
579590
Attr->setImplicitBindingOrderID(ImplicitBindingOrderID);
580-
BufDecl->addAttr(Attr);
591+
D->addAttr(Attr);
581592
}
582593

583594
// Handle end of cbuffer/tbuffer declaration
@@ -600,7 +611,10 @@ void SemaHLSL::ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace) {
600611
if (RBA)
601612
RBA->setImplicitBindingOrderID(OrderID);
602613
else
603-
addImplicitBindingAttrToBuffer(SemaRef, BufDecl, OrderID);
614+
addImplicitBindingAttrToDecl(SemaRef, BufDecl,
615+
BufDecl->isCBuffer() ? RegisterType::CBuffer
616+
: RegisterType::SRV,
617+
OrderID);
604618
}
605619

606620
SemaRef.PopDeclContext();
@@ -1906,7 +1920,7 @@ static bool DiagnoseLocalRegisterBinding(Sema &S, SourceLocation &ArgLoc,
19061920
if (const HLSLAttributedResourceType *AttrResType =
19071921
HLSLAttributedResourceType::findHandleTypeOnResource(
19081922
VD->getType().getTypePtr())) {
1909-
if (RegType == getRegisterType(AttrResType->getAttrs().ResourceClass))
1923+
if (RegType == getRegisterType(AttrResType))
19101924
return true;
19111925

19121926
S.Diag(D->getLocation(), diag::err_hlsl_binding_type_mismatch)
@@ -2439,8 +2453,8 @@ void SemaHLSL::ActOnEndOfTranslationUnit(TranslationUnitDecl *TU) {
24392453
HLSLBufferDecl *DefaultCBuffer = HLSLBufferDecl::CreateDefaultCBuffer(
24402454
SemaRef.getASTContext(), SemaRef.getCurLexicalContext(),
24412455
DefaultCBufferDecls);
2442-
addImplicitBindingAttrToBuffer(SemaRef, DefaultCBuffer,
2443-
getNextImplicitBindingOrderID());
2456+
addImplicitBindingAttrToDecl(SemaRef, DefaultCBuffer, RegisterType::CBuffer,
2457+
getNextImplicitBindingOrderID());
24442458
SemaRef.getCurLexicalContext()->addDecl(DefaultCBuffer);
24452459
createHostLayoutStructForBuffer(SemaRef, DefaultCBuffer);
24462460

@@ -3640,6 +3654,24 @@ void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
36403654

36413655
// process explicit bindings
36423656
processExplicitBindingsOnDecl(VD);
3657+
3658+
if (VD->getType()->isHLSLResourceRecordArray()) {
3659+
// If the resource array does not have an explicit binding attribute,
3660+
// create an implicit one. It will be used to transfer implicit binding
3661+
// order_ID to codegen.
3662+
if (!VD->hasAttr<HLSLVkBindingAttr>()) {
3663+
HLSLResourceBindingAttr *RBA = VD->getAttr<HLSLResourceBindingAttr>();
3664+
if (!RBA || !RBA->hasRegisterSlot()) {
3665+
uint32_t OrderID = getNextImplicitBindingOrderID();
3666+
if (RBA)
3667+
RBA->setImplicitBindingOrderID(OrderID);
3668+
else
3669+
addImplicitBindingAttrToDecl(
3670+
SemaRef, VD, getRegisterType(getResourceArrayHandleType(VD)),
3671+
OrderID);
3672+
}
3673+
}
3674+
}
36433675
}
36443676

36453677
deduceAddressSpace(VD);

clang/test/AST/HLSL/resource_binding_attr.hlsl

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -finclude-default-header -ast-dump -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -finclude-default-header -ast-dump -o - %s | FileCheck %s -check-prefixes=CHECK,DXIL
2+
// RUN: %clang_cc1 -triple spirv-unknown-vulkan-library -finclude-default-header -ast-dump -o - %s | FileCheck %s -check-prefixes=CHECK,SPV
23

34
// CHECK: HLSLBufferDecl {{.*}} line:[[# @LINE + 4]]:9 cbuffer CB
45
// CHECK-NEXT: HLSLResourceClassAttr {{.*}} Implicit CBuffer
@@ -34,6 +35,10 @@ RWBuffer<float> UAV1 : register(u2), UAV2 : register(u4);
3435
// CHECK: HLSLResourceBindingAttr {{.*}} "" "space5"
3536
RWBuffer<float> UAV3 : register(space5);
3637

38+
// CHECK: VarDecl {{.*}} UAV_Array 'RWBuffer<float>[10]'
39+
// CHECK: HLSLResourceBindingAttr {{.*}} "u10" "space6"
40+
RWBuffer<float> UAV_Array[10] : register(u10, space6);
41+
3742
//
3843
// Default constants ($Globals) layout annotations
3944

@@ -56,3 +61,44 @@ struct S {
5661
// CHECK: VarDecl {{.*}} s 'hlsl_constant S'
5762
// CHECK: HLSLResourceBindingAttr {{.*}} "c10" "space0
5863
S s : register(c10);
64+
65+
//
66+
// Implicit binding
67+
68+
// Constant buffers should have implicit binding attribute added by SemaHLSL,
69+
// unless the target is SPIR-V and there is [[vk::binding]] attribute.
70+
// CHECK: HLSLBufferDecl {{.*}} line:[[# @LINE + 3]]:9 cbuffer CB2
71+
// CHECK-NEXT: HLSLResourceClassAttr {{.*}} Implicit CBuffer
72+
// CHECK-NEXT: HLSLResourceBindingAttr {{.*}} Implicit "" "0"
73+
cbuffer CB2 {
74+
float4 c;
75+
}
76+
77+
// CHECK: HLSLBufferDecl {{.*}} line:[[# @LINE + 7]]:9 cbuffer CB3
78+
// CHECK-NEXT: HLSLResourceClassAttr {{.*}} Implicit CBuffer
79+
// DXIL: HLSLResourceBindingAttr {{.*}} Implicit
80+
// DXIL-NOT: HLSLVkBindingAttr
81+
// SPV: HLSLVkBindingAttr {{.*}} 1 0
82+
// SPV-NOT: HLSLResourceBindingAttr {{.*}} Implicit
83+
[[vk::binding(1)]]
84+
cbuffer CB3 {
85+
float2 d;
86+
}
87+
88+
// Resource arrays should have implicit binding attribute added by SemaHLSL,
89+
// unless the target is SPIR-V and there is [[vk::binding]] attribute.
90+
// CHECK: VarDecl {{.*}} SB 'StructuredBuffer<float>[10]'
91+
// CHECK: HLSLResourceBindingAttr {{.*}} Implicit "" "0"
92+
StructuredBuffer<float> SB[10];
93+
94+
// CHECK: VarDecl {{.*}} SB2 'StructuredBuffer<float>[10]'
95+
// DXIL: HLSLResourceBindingAttr {{.*}} Implicit
96+
// DXIL-NOT: HLSLVkBindingAttr
97+
// SPV: HLSLVkBindingAttr {{.*}} 2 0
98+
// SPV-NOT: HLSLResourceBindingAttr {{.*}} Implicit
99+
[[vk::binding(2)]]
100+
StructuredBuffer<float> SB2[10];
101+
102+
// $Globals should have implicit binding attribute added by SemaHLSL
103+
// CHECK: HLSLBufferDecl {{.*}} implicit cbuffer $Globals
104+
// CHECK: HLSLResourceBindingAttr {{.*}} Implicit "" "0"

clang/test/CodeGen/kcfi-generalize.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=kcfi -fsanitize-trap=kcfi -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=UNGENERALIZED %s
2+
// RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=kcfi -fsanitize-trap=kcfi -fsanitize-cfi-icall-generalize-pointers -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=GENERALIZED %s
3+
4+
// Test that const char* is generalized to const ptr and that char** is
5+
// generalized to ptr
6+
7+
// CHECK: define{{.*}} ptr @f({{.*}} !kcfi_type [[TYPE:![0-9]+]]
8+
int** f(const char *a, const char **b) {
9+
return (int**)0;
10+
}
11+
12+
// GENERALIZED: define{{.*}} ptr @f2({{.*}} !kcfi_type [[TYPE]]
13+
// UNGENERALIZED: define{{.*}} ptr @f2({{.*}} !kcfi_type [[TYPE2:![0-9]+]]
14+
int** f2(const int *a, const int **b) {
15+
return (int**)0;
16+
}
17+
18+
// CHECK: define{{.*}} ptr @f3({{.*}} !kcfi_type [[TYPE3:![0-9]+]]
19+
int** f3(char *a, char **b) {
20+
return (int**)0;
21+
}
22+
23+
void g(int** (*fp)(const char *, const char **)) {
24+
// UNGENERALIZED: call {{.*}} [ "kcfi"(i32 1296635908) ]
25+
// GENERALIZED: call {{.*}} [ "kcfi"(i32 -49168686) ]
26+
fp(0, 0);
27+
}
28+
29+
// UNGENERALIZED: [[TYPE]] = !{i32 1296635908}
30+
// GENERALIZED: [[TYPE]] = !{i32 -49168686}
31+
32+
// UNGENERALIZED: [[TYPE3]] = !{i32 874141567}
33+
// GENERALIZED: [[TYPE3]] = !{i32 954385378}

clang/test/CodeGenHLSL/builtins/dot2add.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang_cc1 -finclude-default-header -fnative-half-type -triple \
2-
// RUN: dxil-pc-shadermodel6.3-compute %s -emit-llvm -o - | \
2+
// RUN: dxil-pc-shadermodel6.4-compute %s -emit-llvm -o - | \
33
// RUN: FileCheck %s --check-prefixes=CHECK,CHECK-DXIL
44
// RUN: %clang_cc1 -finclude-default-header -fnative-half-type -triple \
55
// RUN: spirv-pc-vulkan-compute %s -emit-llvm -o - | \

clang/test/Driver/fsanitize.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,11 @@
794794
// RUN: not %clang --target=x86_64-linux-gnu -fsanitize=cfi-icall -fsanitize-cfi-icall-generalize-pointers -fsanitize-cfi-cross-dso -fvisibility=hidden -flto -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI-GENERALIZE-AND-CROSS-DSO
795795
// CHECK-CFI-GENERALIZE-AND-CROSS-DSO: error: invalid argument '-fsanitize-cfi-cross-dso' not allowed with '-fsanitize-cfi-icall-generalize-pointers'
796796

797+
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=kcfi -fsanitize-cfi-icall-generalize-pointers -fvisibility=hidden -flto -c -resource-dir=%S/Inputs/resource_dir %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-KCFI-GENERALIZE-POINTERS
798+
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=kcfi -fvisibility=hidden -flto -c -resource-dir=%S/Inputs/resource_dir %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-KCFI-GENERALIZE-POINTERS
799+
// CHECK-KCFI-GENERALIZE-POINTERS: -fsanitize-cfi-icall-generalize-pointers
800+
// CHECK-NO-KCFI-GENERALIZE-POINTERS-NOT: -fsanitize-cfi-icall-generalize-pointers
801+
797802
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=cfi-icall -fsanitize-cfi-canonical-jump-tables -fvisibility=hidden -flto -c -resource-dir=%S/Inputs/resource_dir %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI-CANONICAL-JUMP-TABLES
798803
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=cfi-icall -fno-sanitize-cfi-canonical-jump-tables -fvisibility=hidden -flto -c %s -resource-dir=%S/Inputs/resource_dir -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-CFI-CANONICAL-JUMP-TABLES
799804
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=cfi-icall -fvisibility=hidden -flto -c -resource-dir=%S/Inputs/resource_dir %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI-CANONICAL-JUMP-TABLES

0 commit comments

Comments
 (0)