171e36426SJustin Bogner //===- DXILUpgrade.cpp - Upgrade DXIL metadata to LLVM constructs ---------===// 271e36426SJustin Bogner // 371e36426SJustin Bogner // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 471e36426SJustin Bogner // See https://llvm.org/LICENSE.txt for license information. 571e36426SJustin Bogner // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 671e36426SJustin Bogner // 771e36426SJustin Bogner //===----------------------------------------------------------------------===// 871e36426SJustin Bogner 971e36426SJustin Bogner #include "llvm/Transforms/Utils/DXILUpgrade.h" 10*1f3d70a9SJustin Bogner #include "llvm/IR/Constants.h" 11*1f3d70a9SJustin Bogner #include "llvm/IR/Metadata.h" 12*1f3d70a9SJustin Bogner #include "llvm/IR/Module.h" 13*1f3d70a9SJustin Bogner #include "llvm/Support/Debug.h" 1471e36426SJustin Bogner 1571e36426SJustin Bogner using namespace llvm; 1671e36426SJustin Bogner 17*1f3d70a9SJustin Bogner #define DEBUG_TYPE "dxil-upgrade" 18*1f3d70a9SJustin Bogner handleValVerMetadata(Module & M)1971e36426SJustin Bognerstatic bool handleValVerMetadata(Module &M) { 2071e36426SJustin Bogner NamedMDNode *ValVer = M.getNamedMetadata("dx.valver"); 2171e36426SJustin Bogner if (!ValVer) 2271e36426SJustin Bogner return false; 2371e36426SJustin Bogner 24*1f3d70a9SJustin Bogner LLVM_DEBUG({ 25*1f3d70a9SJustin Bogner MDNode *N = ValVer->getOperand(0); 26*1f3d70a9SJustin Bogner auto X = mdconst::extract<ConstantInt>(N->getOperand(0))->getZExtValue(); 27*1f3d70a9SJustin Bogner auto Y = mdconst::extract<ConstantInt>(N->getOperand(1))->getZExtValue(); 28*1f3d70a9SJustin Bogner dbgs() << "DXIL: validation version: " << X << "." << Y << "\n"; 29*1f3d70a9SJustin Bogner }); 3071e36426SJustin Bogner // We don't need the validation version internally, so we drop it. 3171e36426SJustin Bogner ValVer->dropAllReferences(); 3271e36426SJustin Bogner ValVer->eraseFromParent(); 3371e36426SJustin Bogner return true; 3471e36426SJustin Bogner } 3571e36426SJustin Bogner run(Module & M,ModuleAnalysisManager & AM)3671e36426SJustin BognerPreservedAnalyses DXILUpgradePass::run(Module &M, ModuleAnalysisManager &AM) { 3771e36426SJustin Bogner PreservedAnalyses PA; 3871e36426SJustin Bogner // We never add, remove, or change functions here. 3971e36426SJustin Bogner PA.preserve<FunctionAnalysisManagerModuleProxy>(); 4071e36426SJustin Bogner PA.preserveSet<AllAnalysesOn<Function>>(); 4171e36426SJustin Bogner 4271e36426SJustin Bogner bool Changed = false; 4371e36426SJustin Bogner Changed |= handleValVerMetadata(M); 4471e36426SJustin Bogner 4571e36426SJustin Bogner if (!Changed) 4671e36426SJustin Bogner return PreservedAnalyses::all(); 4771e36426SJustin Bogner return PA; 4871e36426SJustin Bogner } 49