@@ -260,135 +260,6 @@ void CGHLSLRuntime::finishCodeGen() {
260260 generateGlobalCtorDtorCalls ();
261261}
262262
263- void CGHLSLRuntime::addBufferResourceAnnotation (llvm::GlobalVariable *GV,
264- llvm::hlsl::ResourceClass RC,
265- llvm::hlsl::ResourceKind RK,
266- bool IsROV,
267- llvm::hlsl::ElementType ET,
268- BufferResBinding &Binding) {
269- llvm::Module &M = CGM.getModule ();
270-
271- NamedMDNode *ResourceMD = nullptr ;
272- switch (RC) {
273- case llvm::hlsl::ResourceClass::UAV:
274- ResourceMD = M.getOrInsertNamedMetadata (" hlsl.uavs" );
275- break ;
276- case llvm::hlsl::ResourceClass::SRV:
277- ResourceMD = M.getOrInsertNamedMetadata (" hlsl.srvs" );
278- break ;
279- case llvm::hlsl::ResourceClass::CBuffer:
280- ResourceMD = M.getOrInsertNamedMetadata (" hlsl.cbufs" );
281- break ;
282- default :
283- assert (false && " Unsupported buffer type!" );
284- return ;
285- }
286- assert (ResourceMD != nullptr &&
287- " ResourceMD must have been set by the switch above." );
288-
289- llvm::hlsl::FrontendResource Res (
290- GV, RK, ET, IsROV, Binding.Reg .value_or (UINT_MAX), Binding.Space );
291- ResourceMD->addOperand (Res.getMetadata ());
292- }
293-
294- static llvm::hlsl::ElementType
295- calculateElementType (const ASTContext &Context, const clang::Type *ResourceTy) {
296- using llvm::hlsl::ElementType;
297-
298- // TODO: We may need to update this when we add things like ByteAddressBuffer
299- // that don't have a template parameter (or, indeed, an element type).
300- const auto *TST = ResourceTy->getAs <TemplateSpecializationType>();
301- assert (TST && " Resource types must be template specializations" );
302- ArrayRef<TemplateArgument> Args = TST->template_arguments ();
303- assert (!Args.empty () && " Resource has no element type" );
304-
305- // At this point we have a resource with an element type, so we can assume
306- // that it's valid or we would have diagnosed the error earlier.
307- QualType ElTy = Args[0 ].getAsType ();
308-
309- // We should either have a basic type or a vector of a basic type.
310- if (const auto *VecTy = ElTy->getAs <clang::VectorType>())
311- ElTy = VecTy->getElementType ();
312-
313- if (ElTy->isSignedIntegerType ()) {
314- switch (Context.getTypeSize (ElTy)) {
315- case 16 :
316- return ElementType::I16;
317- case 32 :
318- return ElementType::I32;
319- case 64 :
320- return ElementType::I64;
321- }
322- } else if (ElTy->isUnsignedIntegerType ()) {
323- switch (Context.getTypeSize (ElTy)) {
324- case 16 :
325- return ElementType::U16;
326- case 32 :
327- return ElementType::U32;
328- case 64 :
329- return ElementType::U64;
330- }
331- } else if (ElTy->isSpecificBuiltinType (BuiltinType::Half))
332- return ElementType::F16;
333- else if (ElTy->isSpecificBuiltinType (BuiltinType::Float))
334- return ElementType::F32;
335- else if (ElTy->isSpecificBuiltinType (BuiltinType::Double))
336- return ElementType::F64;
337-
338- // TODO: We need to handle unorm/snorm float types here once we support them
339- llvm_unreachable (" Invalid element type for resource" );
340- }
341-
342- void CGHLSLRuntime::annotateHLSLResource (const VarDecl *D, GlobalVariable *GV) {
343- const Type *Ty = D->getType ()->getPointeeOrArrayElementType ();
344- if (!Ty)
345- return ;
346- const auto *RD = Ty->getAsCXXRecordDecl ();
347- if (!RD)
348- return ;
349- // the resource related attributes are on the handle member
350- // inside the record decl
351- for (auto *FD : RD->fields ()) {
352- const auto *HLSLResAttr = FD->getAttr <HLSLResourceAttr>();
353- const HLSLAttributedResourceType *AttrResType =
354- dyn_cast<HLSLAttributedResourceType>(FD->getType ().getTypePtr ());
355- if (!HLSLResAttr || !AttrResType)
356- continue ;
357-
358- llvm::hlsl::ResourceClass RC = AttrResType->getAttrs ().ResourceClass ;
359- if (RC == llvm::hlsl::ResourceClass::UAV ||
360- RC == llvm::hlsl::ResourceClass::SRV)
361- // UAVs and SRVs have already been converted to use LLVM target types,
362- // we can disable generating of these resource annotations. This will
363- // enable progress on structured buffers with user defined types this
364- // resource annotations code does not handle and it crashes.
365- // This whole function is going to be removed as soon as cbuffers are
366- // converted to target types (llvm/llvm-project #114126).
367- return ;
368-
369- bool IsROV = AttrResType->getAttrs ().IsROV ;
370- llvm::hlsl::ResourceKind RK = HLSLResAttr->getResourceKind ();
371- llvm::hlsl::ElementType ET = calculateElementType (CGM.getContext (), Ty);
372-
373- BufferResBinding Binding (D->getAttr <HLSLResourceBindingAttr>());
374- addBufferResourceAnnotation (GV, RC, RK, IsROV, ET, Binding);
375- }
376- }
377-
378- CGHLSLRuntime::BufferResBinding::BufferResBinding (
379- HLSLResourceBindingAttr *Binding) {
380- if (Binding) {
381- llvm::APInt RegInt (64 , 0 );
382- Binding->getSlot ().substr (1 ).getAsInteger (10 , RegInt);
383- Reg = RegInt.getLimitedValue ();
384- llvm::APInt SpaceInt (64 , 0 );
385- Binding->getSpace ().substr (5 ).getAsInteger (10 , SpaceInt);
386- Space = SpaceInt.getLimitedValue ();
387- } else {
388- Space = 0 ;
389- }
390- }
391-
392263void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes (
393264 const FunctionDecl *FD, llvm::Function *Fn) {
394265 const auto *ShaderAttr = FD->getAttr <HLSLShaderAttr>();
0 commit comments