================ @@ -0,0 +1,121 @@ +//===- SPIRVLowerResourceNames.cpp - Lower resource names to metadata -----===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "SPIRV.h" +#include "llvm/ADT/MapVector.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Analysis/ValueTracking.h" +#include "llvm/IR/Dominators.h" +#include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/IntrinsicsSPIRV.h" +#include "llvm/IR/Module.h" +#include "llvm/Pass.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Transforms/Utils/PromoteMemToReg.h" + +using namespace llvm; + +#define DEBUG_TYPE "spirv-lower-resource-names" + +static void promoteNameAllocas(ArrayRef<CallInst *> Intrinsics) { + MapVector<Function *, SmallVector<AllocaInst *, 4>> Allocas; + SmallPtrSet<AllocaInst *, 8> Seen; + for (CallInst *CI : Intrinsics) { + auto *LI = dyn_cast<LoadInst>(CI->getArgOperand(4)); + if (!LI) + continue; + auto *AI = dyn_cast<AllocaInst>(LI->getPointerOperand()); + if (AI && !Seen.insert(AI).second) + continue; + if (AI && AI->isStaticAlloca() && isAllocaPromotable(AI)) + Allocas[CI->getFunction()].push_back(AI); + else + reportFatalInternalError("unresolvable SPIR-V resource name"); + } + for (auto &[F, Names] : Allocas) { + DominatorTree DT(*F); + PromoteMemToReg(Names, DT); + } +} + +static bool lowerResourceNames(Module &M) { + SmallVector<CallInst *, 8> Intrinsics; + for (Function &F : M) { + switch (F.getIntrinsicID()) { + case Intrinsic::spv_resource_handlefrombinding: + case Intrinsic::spv_resource_handlefromimplicitbinding: + for (User *U : F.users()) { + if (auto *CI = dyn_cast<CallInst>(U)) + Intrinsics.push_back(CI); + } + break; + default: + break; + } + } + if (Intrinsics.empty()) + return false; + + promoteNameAllocas(Intrinsics); + + SmallPtrSet<GlobalVariable *, 8> Names; + for (CallInst *CI : Intrinsics) { + Function *F = CI->getCalledFunction(); + Intrinsic::ID MdID = + F->getIntrinsicID() == Intrinsic::spv_resource_handlefrombinding + ? Intrinsic::spv_resource_handlefrombinding_mdname + : Intrinsic::spv_resource_handlefromimplicitbinding_mdname; + Function *MdFn = + Intrinsic::getOrInsertDeclaration(&M, MdID, {F->getReturnType()}); + Value *NamePtr = CI->getArgOperand(4); + StringRef Name; + if (!isa<ConstantPointerNull>(NamePtr) && + !getConstantStringInfo(NamePtr, Name)) + report_fatal_error("expected a constant resource name"); ---------------- dnovillo wrote:
nit: use `reportFatalUsageError` here. `report_fatal_error` is deprecated. https://github.com/llvm/llvm-project/pull/243531 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
