Skip to content

Commit ab0c968

Browse files
Implement support for groupshared arg attribute (#8013)
Implements support for groupshared argument attribute. Closes #7969 --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 5ac9138 commit ab0c968

25 files changed

Lines changed: 384 additions & 30 deletions

docs/ReleaseNotes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ The included licenses apply to the following files:
2929
#### Experimental Shader Model 6.10
3030

3131
- Moved Linear Algebra (Cooperative Vector) DXIL Opcodes to experimental Shader Model 6.10
32-
- Added support for `long long` and `unsigned long long` compile-time constant evaluation, fixes [#7952](https://github.com/microsoft/DirectXShaderCompiler/issues/7952).
3332
- Implement GetGroupWaveIndex and GetGroupWaveCount in experimental Shader Model 6.10.
3433
- [proposal](https://github.com/microsoft/hlsl-specs/blob/main/proposals/0048-group-wave-index.md)
3534
- GetGroupWaveIndex: New intrinsic for Compute, Mesh, Amplification and Node shaders which returns the index of the wave within the thread group that the the thread is executing.
3635
- GetGroupWaveCount: New intrinsic for Compute, Mesh, Amplification and Node
3736
shaders which returns the total number of waves executing within the thread
3837
group.
38+
- Added support for `long long` and `unsigned long long` compile-time constant evaluation, fixes [#7952](https://github.com/microsoft/DirectXShaderCompiler/issues/7952).
3939

4040
#### Noteble SPIR-V updates
4141

lib/HLSL/HLLegalizeParameter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ bool HLLegalizeParameter::runOnModule(Module &M) {
256256
continue;
257257

258258
for (Argument &Arg : F.args()) {
259-
if (!Arg.getType()->isPointerTy())
259+
Type *PtrTy = dyn_cast<PointerType>(Arg.getType());
260+
if (!PtrTy || DXIL::kTGSMAddrSpace == PtrTy->getPointerAddressSpace())
260261
continue;
261262
Type *EltTy = dxilutil::GetArrayEltTy(Arg.getType());
262263
if (dxilutil::IsHLSLObjectType(EltTy) ||

tools/clang/include/clang/AST/Decl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,9 @@ class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
813813
/// Whether the parameter is copied out.
814814
unsigned IsModifierOut : 1;
815815

816+
/// Whether the parameter should be treated as a reference.
817+
unsigned IsModifierRef : 1;
818+
816819
/// The number of parameters preceding this parameter in the
817820
/// function parameter scope in which it was declared.
818821
unsigned ParameterIndex : NumParameterIndexBits;
@@ -1377,6 +1380,7 @@ class ParmVarDecl : public VarDecl {
13771380
assert(ParmVarDeclBits.IsObjCMethodParam == false);
13781381
setDefaultArg(DefArg);
13791382
// HLSL Change Start
1383+
setModifierRef(ParamMod.isRef());
13801384
setModifierIn(ParamMod.isAnyIn());
13811385
setModifierOut(ParamMod.isAnyOut());
13821386
// change to reference type for out param
@@ -1456,8 +1460,12 @@ class ParmVarDecl : public VarDecl {
14561460
void setModifierIn(bool value) { ParmVarDeclBits.IsKNRPromoted = !value; }
14571461
bool isModifierOut() const { return ParmVarDeclBits.IsModifierOut; }
14581462
void setModifierOut(bool value) { ParmVarDeclBits.IsModifierOut = value; }
1463+
bool isModifierRef() const { return ParmVarDeclBits.IsModifierRef; }
1464+
void setModifierRef(bool value) { ParmVarDeclBits.IsModifierRef = value; }
14591465
/// Synthesize a ParameterModifier value for this parameter.
14601466
hlsl::ParameterModifier getParamModifiers() const {
1467+
if (isModifierRef())
1468+
return hlsl::ParameterModifier(hlsl::ParameterModifier::Kind::Ref);
14611469
if (isModifierIn() && !isModifierOut())
14621470
return hlsl::ParameterModifier(hlsl::ParameterModifier::Kind::In);
14631471
if (isModifierIn() && isModifierOut())

tools/clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,13 @@ def warn_hlsl_sometimes_uninit_out_param : Warning<
15971597
"its declaration is reached|"
15981598
"%3 is called}2">,
15991599
InGroup<HLSLParameterUsage>;
1600+
def warn_hlsl_groupshared_202x: Warning<
1601+
"Support for groupshared parameter annotation not added until HLSL 202x">,
1602+
InGroup<HLSLParameterUsage>;
1603+
def warn_hlsl_groupshared_inout: Warning<
1604+
"Passing groupshared variable to a parameter annotated with inout. See "
1605+
"'groupshared' parameter annotation added in 202x.">,
1606+
InGroup<HLSLParameterUsage>;
16001607
// HLSL Change End - Add warning for uninitialized out param
16011608
def warn_maybe_uninit_var : Warning<
16021609
"variable %0 may be uninitialized when "

tools/clang/include/clang/Basic/Specifiers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ namespace hlsl {
295295

296296
unsigned getAsUnsigned() const { return (unsigned)m_Kind; }
297297

298+
bool isRef() const { return m_Kind == Kind::Ref; }
298299
bool isIn() const { return m_Kind == Kind::In; }
299300
bool isOut() const { return m_Kind == Kind::Out; }
300301
bool isInOut() const { return m_Kind == Kind::InOut; }

tools/clang/lib/AST/HlslTypes.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ bool HasHLSLReorderCoherent(clang::QualType type) {
292292
return false;
293293
}
294294

295+
/// Checks whether the pAttributes indicate a parameter is groupshared
296+
bool IsParamAttributedAsGroupShared(clang::AttributeList *pAttributes);
297+
295298
/// Checks whether the pAttributes indicate a parameter is inout or out; if
296299
/// inout, pIsIn will be set to true.
297300
bool IsParamAttributedAsOut(clang::AttributeList *pAttributes, bool *pIsIn);
@@ -934,6 +937,15 @@ unsigned GetHLSLOutputPatchCount(QualType type) {
934937
return argList[1].getAsIntegral().getLimitedValue();
935938
}
936939

940+
bool IsParamAttributedAsGroupShared(clang::AttributeList *pAttributes) {
941+
while (pAttributes != nullptr) {
942+
if (pAttributes->getKind() == AttributeList::AT_HLSLGroupShared)
943+
return true;
944+
pAttributes = pAttributes->getNext();
945+
}
946+
return false;
947+
}
948+
937949
bool IsParamAttributedAsOut(clang::AttributeList *pAttributes, bool *pIsIn) {
938950
bool anyFound = false;
939951
bool inFound = false;
@@ -967,6 +979,8 @@ bool IsParamAttributedAsOut(clang::AttributeList *pAttributes, bool *pIsIn) {
967979

968980
hlsl::ParameterModifier
969981
ParamModFromAttributeList(clang::AttributeList *pAttributes) {
982+
if (IsParamAttributedAsGroupShared(pAttributes))
983+
return ParameterModifier(hlsl::ParameterModifier::Kind::Ref);
970984
bool isIn, isOut;
971985
isOut = IsParamAttributedAsOut(pAttributes, &isIn);
972986
return ParameterModifier::FromInOut(isIn, isOut);

tools/clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "dxc/DXIL/DxilConstants.h"
15+
1416
#include "clang/AST/Mangle.h"
1517
#include "clang/AST/ASTContext.h"
1618
#include "clang/AST/Attr.h"
@@ -2033,6 +2035,9 @@ void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T,
20332035
Qualifiers Quals, SourceRange Range) {
20342036
QualType PointeeType = T->getPointeeType();
20352037
Out << (Quals.hasVolatile() ? 'B' : 'A');
2038+
if (PointeeType.getQualifiers().getAddressSpace() ==
2039+
hlsl::DXIL::kTGSMAddrSpace)
2040+
Out << 'G';
20362041
manglePointerExtQualifiers(Quals, PointeeType);
20372042
mangleType(PointeeType, Range);
20382043
}

tools/clang/lib/CodeGen/CGHLSLMS.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,9 @@ unsigned CGMSHLSLRuntime::AddTypeAnnotation(QualType Ty,
12451245
if (const ReferenceType *RefType = dyn_cast<ReferenceType>(paramTy))
12461246
paramTy = RefType->getPointeeType();
12471247

1248+
if (const ReferenceType *RefType = dyn_cast<ReferenceType>(Ty))
1249+
Ty = RefType->getPointeeType();
1250+
12481251
// Get size.
12491252
llvm::Type *Type = CGM.getTypes().ConvertType(paramTy);
12501253
unsigned size = dataLayout.getTypeAllocSize(Type);
@@ -6245,7 +6248,8 @@ void CGMSHLSLRuntime::EmitHLSLOutParamConversionInit(
62456248
}
62466249
} else if (isAggregateType) {
62476250
// aggregate in-only - emit RValue, unless LValueToRValue cast
6248-
EmitRValueAgg = true;
6251+
if (Param->isModifierIn())
6252+
EmitRValueAgg = true;
62496253
if (const ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(Arg)) {
62506254
if (cast->getCastKind() == CastKind::CK_LValueToRValue) {
62516255
EmitRValueAgg = false;

tools/clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4945,6 +4945,12 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
49454945
handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr);
49464946
break;
49474947
case AttributeList::AT_NoInline:
4948+
if (S.LangOpts.HLSL) {
4949+
bool Handled = false;
4950+
hlsl::HandleDeclAttributeForHLSL(S, D, Attr, Handled);
4951+
if (Handled)
4952+
break;
4953+
}
49484954
handleSimpleAttribute<NoInlineAttr>(S, D, Attr);
49494955
break;
49504956
case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg.

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14490,6 +14490,21 @@ void Sema::DiagnoseHLSLDeclAttr(const Decl *D, const Attr *A) {
1449014490
HLSLExternalSource *ExtSource = HLSLExternalSource::FromSema(this);
1449114491
const bool IsGCAttr = isa<HLSLGloballyCoherentAttr>(A);
1449214492
const bool IsRCAttr = isa<HLSLReorderCoherentAttr>(A);
14493+
const bool IsExportAttr = isa<HLSLExportAttr>(A);
14494+
const bool IsNoInlineAttr = isa<NoInlineAttr>(A);
14495+
if (IsExportAttr || IsNoInlineAttr) {
14496+
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
14497+
for (ParmVarDecl *PVD : FD->parameters()) {
14498+
if (PVD->hasAttr<HLSLGroupSharedAttr>()) {
14499+
Diag(A->getLocation(), diag::err_hlsl_varmodifiersna)
14500+
<< "groupshared"
14501+
<< "export/noinline"
14502+
<< "parameter";
14503+
return;
14504+
}
14505+
}
14506+
}
14507+
}
1449314508
if (IsGCAttr || IsRCAttr) {
1449414509
const ValueDecl *TD = cast<ValueDecl>(D);
1449514510
if (TD->getType()->isDependentType())
@@ -14671,6 +14686,8 @@ void hlsl::HandleDeclAttributeForHLSL(Sema &S, Decl *D, const AttributeList &A,
1467114686
VD->setType(
1467214687
S.Context.getAddrSpaceQualType(VD->getType(), DXIL::kTGSMAddrSpace));
1467314688
}
14689+
if (ParmVarDecl *VD = dyn_cast<ParmVarDecl>(D))
14690+
VD->setType(S.Context.getLValueReferenceType(VD->getType()));
1467414691
break;
1467514692
case AttributeList::AT_HLSLGroupSharedLimit:
1467614693
declAttr = ::new (S.Context) HLSLGroupSharedLimitAttr(
@@ -15017,6 +15034,7 @@ void hlsl::HandleDeclAttributeForHLSL(Sema &S, Decl *D, const AttributeList &A,
1501715034
}
1501815035

1501915036
if (declAttr != nullptr) {
15037+
S.DiagnoseHLSLDeclAttr(D, declAttr);
1502015038
DXASSERT_NOMSG(Handled);
1502115039
D->addAttr(declAttr);
1502215040

@@ -15770,7 +15788,17 @@ bool Sema::DiagnoseHLSLDecl(Declarator &D, DeclContext *DC, Expr *BitWidth,
1577015788
break;
1577115789
case AttributeList::AT_HLSLGroupShared:
1577215790
isGroupShared = true;
15773-
if (!isGlobal) {
15791+
if (isParameter && getLangOpts().HLSLVersion < hlsl::LangStd::v202x)
15792+
Diag(pAttr->getLoc(), diag::warn_hlsl_groupshared_202x);
15793+
if (isParameter && (usageIn || usageOut)) {
15794+
Diag(pAttr->getLoc(), diag::err_hlsl_varmodifiersna)
15795+
<< (usageIn && usageOut ? "'inout'"
15796+
: usageIn ? "'in'"
15797+
: "'out'")
15798+
<< pAttr->getName() << declarationType;
15799+
result = false;
15800+
}
15801+
if (!(isGlobal || isParameter)) {
1577415802
Diag(pAttr->getLoc(), diag::err_hlsl_varmodifierna)
1577515803
<< pAttr->getName() << declarationType << pAttr->getRange();
1577615804
result = false;
@@ -15807,6 +15835,10 @@ bool Sema::DiagnoseHLSLDecl(Declarator &D, DeclContext *DC, Expr *BitWidth,
1580715835
Diag(pAttr->getLoc(), diag::err_hlsl_usage_not_on_parameter)
1580815836
<< pAttr->getName() << pAttr->getRange();
1580915837
result = false;
15838+
} else if (isGroupShared) {
15839+
Diag(pAttr->getLoc(), diag::err_hlsl_varmodifiersna)
15840+
<< pAttr->getName() << "'groupshared'" << declarationType;
15841+
result = false;
1581015842
}
1581115843
if (!IsUsageAttributeCompatible(pAttr->getKind(), usageIn, usageOut)) {
1581215844
Diag(pAttr->getLoc(), diag::err_hlsl_duplicate_parameter_usages)

0 commit comments

Comments
 (0)