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