Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions clang/include/clang/AST/HLSLResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ struct ResourceBindingAttrs {
assert(hasBinding() && !isExplicit() && !hasImplicitOrderID());
RegBinding->setImplicitBindingOrderID(Value);
}
void setCounterImplicitOrderID(unsigned Value) const {
assert(hasBinding() && !hasCounterImplicitOrderID());
RegBinding->setImplicitCounterBindingOrderID(Value);
}

bool hasCounterImplicitOrderID() const {
return RegBinding && RegBinding->hasImplicitCounterBindingOrderID();
}

unsigned getCounterImplicitOrderID() const {
assert(hasCounterImplicitOrderID());
return RegBinding->getImplicitCounterBindingOrderID();
}
};

} // namespace hlsl
Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -4944,6 +4944,7 @@ def HLSLResourceBinding: InheritableAttr {
std::optional<unsigned> SlotNumber;
unsigned SpaceNumber;
std::optional<unsigned> ImplicitBindingOrderID;
std::optional<unsigned> ImplicitCounterBindingOrderID;

public:
void setBinding(RegisterType RT, std::optional<unsigned> SlotNum, unsigned SpaceNum) {
Expand Down Expand Up @@ -4976,6 +4977,17 @@ def HLSLResourceBinding: InheritableAttr {
assert(hasImplicitBindingOrderID() && "attribute does not have implicit binding order id");
return ImplicitBindingOrderID.value();
}
void setImplicitCounterBindingOrderID(uint32_t Value) {
assert(!hasImplicitCounterBindingOrderID() && "attribute already has implicit counter binding order id");
ImplicitCounterBindingOrderID = Value;
}
bool hasImplicitCounterBindingOrderID() const {
return ImplicitCounterBindingOrderID.has_value();
}
uint32_t getImplicitCounterBindingOrderID() const {
assert(hasImplicitCounterBindingOrderID() && "attribute does not have implicit counter binding order id");
return ImplicitCounterBindingOrderID.value();
}
}];
}

Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Basic/Builtins.td
Original file line number Diff line number Diff line change
Expand Up @@ -4945,6 +4945,12 @@ def HLSLResourceHandleFromImplicitBinding : LangBuiltin<"HLSL_LANG"> {
let Prototype = "void(...)";
}

def HLSLResourceCounterHandleFromImplicitBinding : LangBuiltin<"HLSL_LANG"> {
let Spellings = ["__builtin_hlsl_resource_counterhandlefromimplicitbinding"];
let Attributes = [NoThrow, CustomTypeChecking];
let Prototype = "void(...)";
}

def HLSLResourceNonUniformIndex : LangBuiltin<"HLSL_LANG"> {
let Spellings = ["__builtin_hlsl_resource_nonuniformindex"];
let Attributes = [NoThrow];
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/CodeGen/CGHLSLBuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,19 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
SmallVector<Value *> Args{OrderID, SpaceOp, RangeOp, IndexOp, Name};
return Builder.CreateIntrinsic(HandleTy, IntrinsicID, Args);
}
case Builtin::BI__builtin_hlsl_resource_counterhandlefromimplicitbinding: {
llvm::Type *HandleTy = CGM.getTypes().ConvertType(E->getType());
Value *MainHandle = EmitScalarExpr(E->getArg(0));
if (CGM.getTriple().isSPIRV()) {
Value *OrderID = EmitScalarExpr(E->getArg(1));
Value *SpaceOp = EmitScalarExpr(E->getArg(2));
llvm::Intrinsic::ID IntrinsicID =
llvm::Intrinsic::spv_resource_counterhandlefromimplicitbinding;
SmallVector<Value *> Args{MainHandle, OrderID, SpaceOp};
return Builder.CreateIntrinsic(HandleTy, IntrinsicID, Args);
}
return MainHandle;
}
case Builtin::BI__builtin_hlsl_resource_nonuniformindex: {
Value *IndexOp = EmitScalarExpr(E->getArg(0));
llvm::Type *RetTy = ConvertType(E->getType());
Expand Down
21 changes: 18 additions & 3 deletions clang/lib/CodeGen/CGHLSLRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,34 @@ static CXXMethodDecl *lookupResourceInitMethodAndSetupArgs(
// explicit binding
auto *RegSlot = llvm::ConstantInt::get(CGM.IntTy, Binding.getSlot());
Args.add(RValue::get(RegSlot), AST.UnsignedIntTy);
CreateMethod = lookupMethod(ResourceDecl, "__createFromBinding", SC_Static);
if (Binding.hasCounterImplicitOrderID())
CreateMethod = lookupMethod(
ResourceDecl, "__createFromBindingWithImplicitCounter", SC_Static);
else
CreateMethod =
lookupMethod(ResourceDecl, "__createFromBinding", SC_Static);
} else {
// implicit binding
auto *OrderID =
llvm::ConstantInt::get(CGM.IntTy, Binding.getImplicitOrderID());
Args.add(RValue::get(OrderID), AST.UnsignedIntTy);
CreateMethod =
lookupMethod(ResourceDecl, "__createFromImplicitBinding", SC_Static);
if (Binding.hasCounterImplicitOrderID())
CreateMethod = lookupMethod(
ResourceDecl, "__createFromImplicitBindingWithImplicitCounter",
SC_Static);
else
CreateMethod =
lookupMethod(ResourceDecl, "__createFromImplicitBinding", SC_Static);
}
Args.add(RValue::get(Space), AST.UnsignedIntTy);
Args.add(RValue::get(Range), AST.IntTy);
Args.add(RValue::get(Index), AST.UnsignedIntTy);
Args.add(RValue::get(NameStr), AST.getPointerType(AST.CharTy.withConst()));
if (Binding.hasCounterImplicitOrderID()) {
uint32_t CounterBinding = Binding.getCounterImplicitOrderID();
auto *CounterOrderID = llvm::ConstantInt::get(CGM.IntTy, CounterBinding);
Args.add(RValue::get(CounterOrderID), AST.UnsignedIntTy);
}

return CreateMethod;
}
Expand Down
112 changes: 107 additions & 5 deletions clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ struct BuiltinTypeMethodBuilder {
_2,
_3,
_4,
_5,
Handle = 128,
CounterHandle,
LastStmt
Expand Down Expand Up @@ -190,6 +191,9 @@ struct BuiltinTypeMethodBuilder {
template <typename T>
BuiltinTypeMethodBuilder &
accessCounterHandleFieldOnResource(T ResourceRecord);
template <typename ResourceT, typename ValueT>
BuiltinTypeMethodBuilder &
setCounterHandleFieldOnResource(ResourceT ResourceRecord, ValueT HandleValue);
template <typename T> BuiltinTypeMethodBuilder &returnValue(T ReturnValue);
BuiltinTypeMethodBuilder &returnThis();
BuiltinTypeDeclBuilder &finalize();
Expand All @@ -205,6 +209,11 @@ struct BuiltinTypeMethodBuilder {
if (!Method)
createDecl();
}

template <typename ResourceT, typename ValueT>
BuiltinTypeMethodBuilder &setFieldOnResource(ResourceT ResourceRecord,
ValueT HandleValue,
FieldDecl *HandleField);
};

TemplateParameterListBuilder::~TemplateParameterListBuilder() {
Expand Down Expand Up @@ -592,13 +601,27 @@ template <typename ResourceT, typename ValueT>
BuiltinTypeMethodBuilder &
BuiltinTypeMethodBuilder::setHandleFieldOnResource(ResourceT ResourceRecord,
ValueT HandleValue) {
return setFieldOnResource(ResourceRecord, HandleValue,
DeclBuilder.getResourceHandleField());
}

template <typename ResourceT, typename ValueT>
BuiltinTypeMethodBuilder &
BuiltinTypeMethodBuilder::setCounterHandleFieldOnResource(
ResourceT ResourceRecord, ValueT HandleValue) {
return setFieldOnResource(ResourceRecord, HandleValue,
DeclBuilder.getResourceCounterHandleField());
}

template <typename ResourceT, typename ValueT>
BuiltinTypeMethodBuilder &BuiltinTypeMethodBuilder::setFieldOnResource(
ResourceT ResourceRecord, ValueT HandleValue, FieldDecl *HandleField) {
ensureCompleteDecl();

Expr *ResourceExpr = convertPlaceholder(ResourceRecord);
Expr *HandleValueExpr = convertPlaceholder(HandleValue);

ASTContext &AST = DeclBuilder.SemaRef.getASTContext();
FieldDecl *HandleField = DeclBuilder.getResourceHandleField();
MemberExpr *HandleMemberExpr = MemberExpr::CreateImplicit(
AST, ResourceExpr, false, HandleField, HandleField->getType(), VK_LValue,
OK_Ordinary);
Expand Down Expand Up @@ -829,6 +852,18 @@ BuiltinTypeDeclBuilder &BuiltinTypeDeclBuilder::addDefaultHandleConstructor() {
.finalize();
}

BuiltinTypeDeclBuilder &
BuiltinTypeDeclBuilder::addStaticInitializationFunctions(bool HasCounter) {
if (HasCounter) {
addCreateFromBindingWithImplicitCounter();
addCreateFromImplicitBindingWithImplicitCounter();
} else {
addCreateFromBinding();
addCreateFromImplicitBinding();
}
return *this;
}

// Adds static method that initializes resource from binding:
//
// static Resource<T> __createFromBinding(unsigned registerNo,
Expand Down Expand Up @@ -903,6 +938,73 @@ BuiltinTypeDeclBuilder &BuiltinTypeDeclBuilder::addCreateFromImplicitBinding() {
.finalize();
}

BuiltinTypeDeclBuilder &
BuiltinTypeDeclBuilder::addCreateFromBindingWithImplicitCounter() {
if (Record->isCompleteDefinition())
return *this;

using PH = BuiltinTypeMethodBuilder::PlaceHolder;
ASTContext &AST = SemaRef.getASTContext();
QualType HandleType = getResourceHandleField()->getType();
QualType RecordType = AST.getTypeDeclType(cast<TypeDecl>(Record));
BuiltinTypeMethodBuilder::LocalVar TmpVar("tmp", RecordType);

return BuiltinTypeMethodBuilder(*this,
"__createFromBindingWithImplicitCounter",
RecordType, false, false, SC_Static)
.addParam("registerNo", AST.UnsignedIntTy)
.addParam("spaceNo", AST.UnsignedIntTy)
.addParam("range", AST.IntTy)
.addParam("index", AST.UnsignedIntTy)
.addParam("name", AST.getPointerType(AST.CharTy.withConst()))
.addParam("counterOrderId", AST.UnsignedIntTy)
.declareLocalVar(TmpVar)
.accessHandleFieldOnResource(TmpVar)
.callBuiltin("__builtin_hlsl_resource_handlefrombinding", HandleType,
PH::LastStmt, PH::_0, PH::_1, PH::_2, PH::_3, PH::_4)
.setHandleFieldOnResource(TmpVar, PH::LastStmt)
.accessHandleFieldOnResource(TmpVar)
.callBuiltin("__builtin_hlsl_resource_counterhandlefromimplicitbinding",
HandleType, PH::LastStmt, PH::_5, PH::_1)
.setCounterHandleFieldOnResource(TmpVar, PH::LastStmt)
.returnValue(TmpVar)
.finalize();
}

BuiltinTypeDeclBuilder &
BuiltinTypeDeclBuilder::addCreateFromImplicitBindingWithImplicitCounter() {
if (Record->isCompleteDefinition())
return *this;

using PH = BuiltinTypeMethodBuilder::PlaceHolder;
ASTContext &AST = SemaRef.getASTContext();
QualType HandleType = getResourceHandleField()->getType();
QualType RecordType = AST.getTypeDeclType(cast<TypeDecl>(Record));
BuiltinTypeMethodBuilder::LocalVar TmpVar("tmp", RecordType);

return BuiltinTypeMethodBuilder(
*this, "__createFromImplicitBindingWithImplicitCounter",
RecordType, false, false, SC_Static)
.addParam("orderId", AST.UnsignedIntTy)
.addParam("spaceNo", AST.UnsignedIntTy)
.addParam("range", AST.IntTy)
.addParam("index", AST.UnsignedIntTy)
.addParam("name", AST.getPointerType(AST.CharTy.withConst()))
.addParam("counterOrderId", AST.UnsignedIntTy)
.declareLocalVar(TmpVar)
.accessHandleFieldOnResource(TmpVar)
.callBuiltin("__builtin_hlsl_resource_handlefromimplicitbinding",
HandleType, PH::LastStmt, PH::_0, PH::_1, PH::_2, PH::_3,
PH::_4)
.setHandleFieldOnResource(TmpVar, PH::LastStmt)
.accessHandleFieldOnResource(TmpVar)
.callBuiltin("__builtin_hlsl_resource_counterhandlefromimplicitbinding",
HandleType, PH::LastStmt, PH::_5, PH::_1)
.setCounterHandleFieldOnResource(TmpVar, PH::LastStmt)
.returnValue(TmpVar)
.finalize();
}

BuiltinTypeDeclBuilder &BuiltinTypeDeclBuilder::addCopyConstructor() {
assert(!Record->isCompleteDefinition() && "record is already complete");

Expand Down Expand Up @@ -1048,7 +1150,7 @@ BuiltinTypeDeclBuilder &BuiltinTypeDeclBuilder::addIncrementCounterMethod() {
return BuiltinTypeMethodBuilder(*this, "IncrementCounter",
SemaRef.getASTContext().UnsignedIntTy)
.callBuiltin("__builtin_hlsl_buffer_update_counter", QualType(),
PH::Handle, getConstantIntExpr(1))
PH::CounterHandle, getConstantIntExpr(1))
.finalize();
}

Expand All @@ -1057,7 +1159,7 @@ BuiltinTypeDeclBuilder &BuiltinTypeDeclBuilder::addDecrementCounterMethod() {
return BuiltinTypeMethodBuilder(*this, "DecrementCounter",
SemaRef.getASTContext().UnsignedIntTy)
.callBuiltin("__builtin_hlsl_buffer_update_counter", QualType(),
PH::Handle, getConstantIntExpr(-1))
PH::CounterHandle, getConstantIntExpr(-1))
.finalize();
}

Expand Down Expand Up @@ -1102,7 +1204,7 @@ BuiltinTypeDeclBuilder &BuiltinTypeDeclBuilder::addAppendMethod() {
return BuiltinTypeMethodBuilder(*this, "Append", AST.VoidTy)
.addParam("value", ElemTy)
.callBuiltin("__builtin_hlsl_buffer_update_counter", AST.UnsignedIntTy,
PH::Handle, getConstantIntExpr(1))
PH::CounterHandle, getConstantIntExpr(1))
.callBuiltin("__builtin_hlsl_resource_getpointer",
AST.getPointerType(AddrSpaceElemTy), PH::Handle,
PH::LastStmt)
Expand All @@ -1119,7 +1221,7 @@ BuiltinTypeDeclBuilder &BuiltinTypeDeclBuilder::addConsumeMethod() {
AST.getAddrSpaceQualType(ElemTy, LangAS::hlsl_device);
return BuiltinTypeMethodBuilder(*this, "Consume", ElemTy)
.callBuiltin("__builtin_hlsl_buffer_update_counter", AST.UnsignedIntTy,
PH::Handle, getConstantIntExpr(-1))
PH::CounterHandle, getConstantIntExpr(-1))
.callBuiltin("__builtin_hlsl_resource_getpointer",
AST.getPointerType(AddrSpaceElemTy), PH::Handle,
PH::LastStmt)
Expand Down
7 changes: 5 additions & 2 deletions clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ class BuiltinTypeDeclBuilder {
BuiltinTypeDeclBuilder &addCopyAssignmentOperator();

// Static create methods
BuiltinTypeDeclBuilder &addCreateFromBinding();
BuiltinTypeDeclBuilder &addCreateFromImplicitBinding();
BuiltinTypeDeclBuilder &addStaticInitializationFunctions(bool HasCounter);

// Builtin types methods
BuiltinTypeDeclBuilder &addLoadMethods();
Expand All @@ -96,6 +95,10 @@ class BuiltinTypeDeclBuilder {
BuiltinTypeDeclBuilder &addConsumeMethod();

private:
BuiltinTypeDeclBuilder &addCreateFromBinding();
BuiltinTypeDeclBuilder &addCreateFromImplicitBinding();
BuiltinTypeDeclBuilder &addCreateFromBindingWithImplicitCounter();
BuiltinTypeDeclBuilder &addCreateFromImplicitBindingWithImplicitCounter();
BuiltinTypeDeclBuilder &addResourceMember(StringRef MemberName,
ResourceClass RC, bool IsROV,
bool RawBuffer, bool IsCounter,
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Sema/HLSLExternalSemaSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ static BuiltinTypeDeclBuilder setupBufferType(CXXRecordDecl *Decl, Sema &S,
.addDefaultHandleConstructor()
.addCopyConstructor()
.addCopyAssignmentOperator()
.addCreateFromBinding()
.addCreateFromImplicitBinding();
.addStaticInitializationFunctions(HasCounter);
}

// This function is responsible for constructing the constraint expression for
Expand Down
Loading