Skip to content

Commit c24da83

Browse files
author
Jenkins
committed
merge main into amd-staging
Change-Id: Id2aed4d6734f1bcb1a30f4eb5e2fcbd2d0688117
2 parents 7aaea58 + fffdd9e commit c24da83

File tree

142 files changed

+2201
-677
lines changed

Some content is hidden

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

142 files changed

+2201
-677
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4769,6 +4769,12 @@ def HLSLSelect : LangBuiltin<"HLSL_LANG"> {
47694769
let Prototype = "void(...)";
47704770
}
47714771

4772+
def HLSLSign : LangBuiltin<"HLSL_LANG"> {
4773+
let Spellings = ["__builtin_hlsl_elementwise_sign"];
4774+
let Attributes = [NoThrow, Const];
4775+
let Prototype = "void(...)";
4776+
}
4777+
47724778
// Builtins for XRay.
47734779
def XRayCustomEvent : Builtin {
47744780
let Spellings = ["__xray_customevent"];

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,26 @@ template <class Emitter> class LoopScope final : public LabelScope<Emitter> {
115115
LoopScope(Compiler<Emitter> *Ctx, LabelTy BreakLabel, LabelTy ContinueLabel)
116116
: LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
117117
OldContinueLabel(Ctx->ContinueLabel),
118-
OldLabelVarScope(Ctx->LabelVarScope) {
118+
OldBreakVarScope(Ctx->BreakVarScope),
119+
OldContinueVarScope(Ctx->ContinueVarScope) {
119120
this->Ctx->BreakLabel = BreakLabel;
120121
this->Ctx->ContinueLabel = ContinueLabel;
121-
this->Ctx->LabelVarScope = this->Ctx->VarScope;
122+
this->Ctx->BreakVarScope = this->Ctx->VarScope;
123+
this->Ctx->ContinueVarScope = this->Ctx->VarScope;
122124
}
123125

124126
~LoopScope() {
125127
this->Ctx->BreakLabel = OldBreakLabel;
126128
this->Ctx->ContinueLabel = OldContinueLabel;
127-
this->Ctx->LabelVarScope = OldLabelVarScope;
129+
this->Ctx->ContinueVarScope = OldContinueVarScope;
130+
this->Ctx->BreakVarScope = OldBreakVarScope;
128131
}
129132

130133
private:
131134
OptLabelTy OldBreakLabel;
132135
OptLabelTy OldContinueLabel;
133-
VariableScope<Emitter> *OldLabelVarScope;
136+
VariableScope<Emitter> *OldBreakVarScope;
137+
VariableScope<Emitter> *OldContinueVarScope;
134138
};
135139

136140
// Sets the context for a switch scope, mapping labels.
@@ -145,18 +149,18 @@ template <class Emitter> class SwitchScope final : public LabelScope<Emitter> {
145149
: LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
146150
OldDefaultLabel(this->Ctx->DefaultLabel),
147151
OldCaseLabels(std::move(this->Ctx->CaseLabels)),
148-
OldLabelVarScope(Ctx->LabelVarScope) {
152+
OldLabelVarScope(Ctx->BreakVarScope) {
149153
this->Ctx->BreakLabel = BreakLabel;
150154
this->Ctx->DefaultLabel = DefaultLabel;
151155
this->Ctx->CaseLabels = std::move(CaseLabels);
152-
this->Ctx->LabelVarScope = this->Ctx->VarScope;
156+
this->Ctx->BreakVarScope = this->Ctx->VarScope;
153157
}
154158

155159
~SwitchScope() {
156160
this->Ctx->BreakLabel = OldBreakLabel;
157161
this->Ctx->DefaultLabel = OldDefaultLabel;
158162
this->Ctx->CaseLabels = std::move(OldCaseLabels);
159-
this->Ctx->LabelVarScope = OldLabelVarScope;
163+
this->Ctx->BreakVarScope = OldLabelVarScope;
160164
}
161165

162166
private:
@@ -4605,18 +4609,23 @@ bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
46054609
this->fallthrough(CondLabel);
46064610
this->emitLabel(CondLabel);
46074611

4608-
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
4609-
if (!visitDeclStmt(CondDecl))
4610-
return false;
4612+
{
4613+
LocalScope<Emitter> CondScope(this);
4614+
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
4615+
if (!visitDeclStmt(CondDecl))
4616+
return false;
46114617

4612-
if (!this->visitBool(Cond))
4613-
return false;
4614-
if (!this->jumpFalse(EndLabel))
4615-
return false;
4618+
if (!this->visitBool(Cond))
4619+
return false;
4620+
if (!this->jumpFalse(EndLabel))
4621+
return false;
46164622

4617-
if (!this->visitStmt(Body))
4618-
return false;
4623+
if (!this->visitStmt(Body))
4624+
return false;
46194625

4626+
if (!CondScope.destroyLocals())
4627+
return false;
4628+
}
46204629
if (!this->jump(CondLabel))
46214630
return false;
46224631
this->fallthrough(EndLabel);
@@ -4636,13 +4645,18 @@ template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
46364645

46374646
this->fallthrough(StartLabel);
46384647
this->emitLabel(StartLabel);
4648+
46394649
{
4650+
LocalScope<Emitter> CondScope(this);
46404651
if (!this->visitStmt(Body))
46414652
return false;
46424653
this->fallthrough(CondLabel);
46434654
this->emitLabel(CondLabel);
46444655
if (!this->visitBool(Cond))
46454656
return false;
4657+
4658+
if (!CondScope.destroyLocals())
4659+
return false;
46464660
}
46474661
if (!this->jumpTrue(StartLabel))
46484662
return false;
@@ -4671,29 +4685,33 @@ bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
46714685
this->fallthrough(CondLabel);
46724686
this->emitLabel(CondLabel);
46734687

4674-
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
4675-
if (!visitDeclStmt(CondDecl))
4676-
return false;
4688+
{
4689+
LocalScope<Emitter> CondScope(this);
4690+
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
4691+
if (!visitDeclStmt(CondDecl))
4692+
return false;
46774693

4678-
if (Cond) {
4679-
if (!this->visitBool(Cond))
4680-
return false;
4681-
if (!this->jumpFalse(EndLabel))
4682-
return false;
4683-
}
4694+
if (Cond) {
4695+
if (!this->visitBool(Cond))
4696+
return false;
4697+
if (!this->jumpFalse(EndLabel))
4698+
return false;
4699+
}
46844700

4685-
{
46864701
if (Body && !this->visitStmt(Body))
46874702
return false;
46884703

46894704
this->fallthrough(IncLabel);
46904705
this->emitLabel(IncLabel);
46914706
if (Inc && !this->discard(Inc))
46924707
return false;
4693-
}
46944708

4709+
if (!CondScope.destroyLocals())
4710+
return false;
4711+
}
46954712
if (!this->jump(CondLabel))
46964713
return false;
4714+
46974715
this->fallthrough(EndLabel);
46984716
this->emitLabel(EndLabel);
46994717
return true;
@@ -4760,7 +4778,7 @@ bool Compiler<Emitter>::visitBreakStmt(const BreakStmt *S) {
47604778
if (!BreakLabel)
47614779
return false;
47624780

4763-
for (VariableScope<Emitter> *C = VarScope; C != LabelVarScope;
4781+
for (VariableScope<Emitter> *C = VarScope; C != BreakVarScope;
47644782
C = C->getParent())
47654783
C->emitDestruction();
47664784
return this->jump(*BreakLabel);
@@ -4771,8 +4789,8 @@ bool Compiler<Emitter>::visitContinueStmt(const ContinueStmt *S) {
47714789
if (!ContinueLabel)
47724790
return false;
47734791

4774-
for (VariableScope<Emitter> *C = VarScope; C != LabelVarScope;
4775-
C = C->getParent())
4792+
for (VariableScope<Emitter> *C = VarScope;
4793+
C && C->getParent() != ContinueVarScope; C = C->getParent())
47764794
C->emitDestruction();
47774795
return this->jump(*ContinueLabel);
47784796
}
@@ -4781,6 +4799,7 @@ template <class Emitter>
47814799
bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
47824800
const Expr *Cond = S->getCond();
47834801
PrimType CondT = this->classifyPrim(Cond->getType());
4802+
LocalScope<Emitter> LS(this);
47844803

47854804
LabelTy EndLabel = this->getLabel();
47864805
OptLabelTy DefaultLabel = std::nullopt;
@@ -4844,7 +4863,8 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
48444863
if (!this->visitStmt(S->getBody()))
48454864
return false;
48464865
this->emitLabel(EndLabel);
4847-
return true;
4866+
4867+
return LS.destroyLocals();
48484868
}
48494869

48504870
template <class Emitter>

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,12 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
409409
/// Switch case mapping.
410410
CaseMap CaseLabels;
411411

412-
/// Scope to cleanup until when chumping to one of the labels.
413-
VariableScope<Emitter> *LabelVarScope = nullptr;
412+
/// Scope to cleanup until when we see a break statement.
413+
VariableScope<Emitter> *BreakVarScope = nullptr;
414414
/// Point to break to.
415415
OptLabelTy BreakLabel;
416+
/// Scope to cleanup until when we see a continue statement.
417+
VariableScope<Emitter> *ContinueVarScope = nullptr;
416418
/// Point to continue to.
417419
OptLabelTy ContinueLabel;
418420
/// Default case label.
@@ -533,7 +535,7 @@ template <class Emitter> class LocalScope : public VariableScope<Emitter> {
533535
return true;
534536
// Emit destructor calls for local variables of record
535537
// type with a destructor.
536-
for (Scope::Local &Local : this->Ctx->Descriptors[*Idx]) {
538+
for (Scope::Local &Local : llvm::reverse(this->Ctx->Descriptors[*Idx])) {
537539
if (!Local.Desc->isPrimitive() && !Local.Desc->isPrimitiveArray()) {
538540
if (!this->Ctx->emitGetPtrLocal(Local.Offset, E))
539541
return false;

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18878,6 +18878,23 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1887818878
Intrinsic::ID ID = CGM.getHLSLRuntime().getWaveIsFirstLaneIntrinsic();
1887918879
return EmitRuntimeCall(Intrinsic::getDeclaration(&CGM.getModule(), ID));
1888018880
}
18881+
case Builtin::BI__builtin_hlsl_elementwise_sign: {
18882+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18883+
llvm::Type *Xty = Op0->getType();
18884+
llvm::Type *retType = llvm::Type::getInt32Ty(this->getLLVMContext());
18885+
if (Xty->isVectorTy()) {
18886+
auto *XVecTy = E->getArg(0)->getType()->getAs<VectorType>();
18887+
retType = llvm::VectorType::get(
18888+
retType, ElementCount::getFixed(XVecTy->getNumElements()));
18889+
}
18890+
assert((E->getArg(0)->getType()->hasFloatingRepresentation() ||
18891+
E->getArg(0)->getType()->hasSignedIntegerRepresentation()) &&
18892+
"sign operand must have a float or int representation");
18893+
18894+
return Builder.CreateIntrinsic(
18895+
retType, CGM.getHLSLRuntime().getSignIntrinsic(),
18896+
ArrayRef<Value *>{Op0}, nullptr, "hlsl.sign");
18897+
}
1888118898
}
1888218899
return nullptr;
1888318900
}

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,10 @@ Value *ScalarExprEmitter::EmitScalarCast(Value *Src, QualType SrcType,
14551455
return Builder.CreateFPToUI(Src, DstTy, "conv");
14561456
}
14571457

1458+
if ((DstElementTy->is16bitFPTy() && SrcElementTy->is16bitFPTy())) {
1459+
Value *FloatVal = Builder.CreateFPExt(Src, Builder.getFloatTy(), "fpext");
1460+
return Builder.CreateFPTrunc(FloatVal, DstTy, "fptrunc");
1461+
}
14581462
if (DstElementTy->getTypeID() < SrcElementTy->getTypeID())
14591463
return Builder.CreateFPTrunc(Src, DstTy, "conv");
14601464
return Builder.CreateFPExt(Src, DstTy, "conv");

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class CGHLSLRuntime {
8080
GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
8181
GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
8282
GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
83+
GENERATE_HLSL_INTRINSIC_FUNCTION(Sign, sign)
8384
GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
8485
GENERATE_HLSL_INTRINSIC_FUNCTION(FDot, fdot)
8586
GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,5 +1826,76 @@ _HLSL_AVAILABILITY(shadermodel, 6.0)
18261826
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_wave_is_first_lane)
18271827
__attribute__((convergent)) bool WaveIsFirstLane();
18281828

1829+
//===----------------------------------------------------------------------===//
1830+
// sign builtins
1831+
//===----------------------------------------------------------------------===//
1832+
1833+
/// \fn T sign(T Val)
1834+
/// \brief Returns -1 if \a Val is less than zero; 0 if \a Val equals zero; and
1835+
/// 1 if \a Val is greater than zero. \param Val The input value.
1836+
1837+
#ifdef __HLSL_ENABLE_16_BIT
1838+
_HLSL_AVAILABILITY(shadermodel, 6.2)
1839+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1840+
int sign(int16_t);
1841+
_HLSL_AVAILABILITY(shadermodel, 6.2)
1842+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1843+
int2 sign(int16_t2);
1844+
_HLSL_AVAILABILITY(shadermodel, 6.2)
1845+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1846+
int3 sign(int16_t3);
1847+
_HLSL_AVAILABILITY(shadermodel, 6.2)
1848+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1849+
int4 sign(int16_t4);
1850+
#endif
1851+
1852+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1853+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1854+
int sign(half);
1855+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1856+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1857+
int2 sign(half2);
1858+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1859+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1860+
int3 sign(half3);
1861+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
1862+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1863+
int4 sign(half4);
1864+
1865+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1866+
int sign(int);
1867+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1868+
int2 sign(int2);
1869+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1870+
int3 sign(int3);
1871+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1872+
int4 sign(int4);
1873+
1874+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1875+
int sign(float);
1876+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1877+
int2 sign(float2);
1878+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1879+
int3 sign(float3);
1880+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1881+
int4 sign(float4);
1882+
1883+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1884+
int sign(int64_t);
1885+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1886+
int2 sign(int64_t2);
1887+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1888+
int3 sign(int64_t3);
1889+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1890+
int4 sign(int64_t4);
1891+
1892+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1893+
int sign(double);
1894+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1895+
int2 sign(double2);
1896+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1897+
int3 sign(double3);
1898+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
1899+
int4 sign(double4);
18291900
} // namespace hlsl
18301901
#endif //_HLSL_HLSL_INTRINSICS_H_

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "clang/AST/Expr.h"
1717
#include "clang/AST/RecursiveASTVisitor.h"
1818
#include "clang/AST/Type.h"
19+
#include "clang/Basic/Builtins.h"
1920
#include "clang/Basic/DiagnosticSema.h"
2021
#include "clang/Basic/LLVM.h"
2122
#include "clang/Basic/SourceLocation.h"
@@ -1513,6 +1514,14 @@ bool CheckNoDoubleVectors(Sema *S, CallExpr *TheCall) {
15131514
return CheckArgsTypesAreCorrect(S, TheCall, S->Context.FloatTy,
15141515
checkDoubleVector);
15151516
}
1517+
bool CheckFloatingOrSignedIntRepresentation(Sema *S, CallExpr *TheCall) {
1518+
auto checkAllSignedTypes = [](clang::QualType PassedType) -> bool {
1519+
return !PassedType->hasSignedIntegerRepresentation() &&
1520+
!PassedType->hasFloatingRepresentation();
1521+
};
1522+
return CheckArgsTypesAreCorrect(S, TheCall, S->Context.IntTy,
1523+
checkAllSignedTypes);
1524+
}
15161525

15171526
bool CheckUnsignedIntRepresentation(Sema *S, CallExpr *TheCall) {
15181527
auto checkAllUnsignedTypes = [](clang::QualType PassedType) -> bool {
@@ -1726,6 +1735,14 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
17261735
TheCall->setType(ArgTyA);
17271736
break;
17281737
}
1738+
case Builtin::BI__builtin_hlsl_elementwise_sign: {
1739+
if (CheckFloatingOrSignedIntRepresentation(&SemaRef, TheCall))
1740+
return true;
1741+
if (SemaRef.PrepareBuiltinElementwiseMathOneArgCall(TheCall))
1742+
return true;
1743+
SetElementTypeAsReturnType(&SemaRef, TheCall, getASTContext().IntTy);
1744+
break;
1745+
}
17291746
// Note these are llvm builtins that we want to catch invalid intrinsic
17301747
// generation. Normal handling of these builitns will occur elsewhere.
17311748
case Builtin::BI__builtin_elementwise_bitreverse: {

0 commit comments

Comments
 (0)