1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
//===- DXILShaderFlags.cpp - DXIL Shader Flags helper objects -------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file This file contains helper objects and APIs for working with DXIL
/// Shader Flags.
///
//===----------------------------------------------------------------------===//
#include "DXILShaderFlags.h"
#include "DirectX.h"
#include "llvm/ADT/SCCIterator.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/DXILResource.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/IntrinsicsDirectX.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::dxil;
static bool hasUAVsAtEveryStage(const DXILResourceMap &DRM,
const ModuleMetadataInfo &MMDI) {
if (DRM.uavs().empty())
return false;
switch (MMDI.ShaderProfile) {
default:
return false;
case Triple::EnvironmentType::Compute:
case Triple::EnvironmentType::Pixel:
return false;
case Triple::EnvironmentType::Vertex:
case Triple::EnvironmentType::Geometry:
case Triple::EnvironmentType::Hull:
case Triple::EnvironmentType::Domain:
return true;
case Triple::EnvironmentType::Library:
case Triple::EnvironmentType::RayGeneration:
case Triple::EnvironmentType::Intersection:
case Triple::EnvironmentType::AnyHit:
case Triple::EnvironmentType::ClosestHit:
case Triple::EnvironmentType::Miss:
case Triple::EnvironmentType::Callable:
case Triple::EnvironmentType::Mesh:
case Triple::EnvironmentType::Amplification:
return MMDI.ValidatorVersion < VersionTuple(1, 8);
}
}
static bool checkWaveOps(Intrinsic::ID IID) {
// Currently unsupported intrinsics
// case Intrinsic::dx_wave_getlanecount:
// case Intrinsic::dx_wave_allequal:
// case Intrinsic::dx_wave_ballot:
// case Intrinsic::dx_wave_readfirst:
// case Intrinsic::dx_wave_reduce.and:
// case Intrinsic::dx_wave_reduce.or:
// case Intrinsic::dx_wave_reduce.xor:
// case Intrinsic::dx_wave_prefixop:
// case Intrinsic::dx_quad.readat:
// case Intrinsic::dx_quad.readacrossx:
// case Intrinsic::dx_quad.readacrossy:
// case Intrinsic::dx_quad.readacrossdiagonal:
// case Intrinsic::dx_wave_prefixballot:
// case Intrinsic::dx_wave_match:
// case Intrinsic::dx_wavemulti.*:
// case Intrinsic::dx_wavemulti.ballot:
// case Intrinsic::dx_quad.vote:
switch (IID) {
default:
return false;
case Intrinsic::dx_wave_is_first_lane:
case Intrinsic::dx_wave_getlaneindex:
case Intrinsic::dx_wave_any:
case Intrinsic::dx_wave_all:
case Intrinsic::dx_wave_readlane:
case Intrinsic::dx_wave_active_countbits:
// Wave Active Op Variants
case Intrinsic::dx_wave_reduce_sum:
case Intrinsic::dx_wave_reduce_usum:
case Intrinsic::dx_wave_reduce_max:
case Intrinsic::dx_wave_reduce_umax:
return true;
}
}
/// Update the shader flags mask based on the given instruction.
/// \param CSF Shader flags mask to update.
/// \param I Instruction to check.
void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
const Instruction &I,
DXILResourceTypeMap &DRTM,
const ModuleMetadataInfo &MMDI) {
if (!CSF.Doubles)
CSF.Doubles = I.getType()->isDoubleTy();
if (!CSF.Doubles) {
for (const Value *Op : I.operands()) {
if (Op->getType()->isDoubleTy()) {
CSF.Doubles = true;
break;
}
}
}
if (CSF.Doubles) {
switch (I.getOpcode()) {
case Instruction::FDiv:
case Instruction::UIToFP:
case Instruction::SIToFP:
case Instruction::FPToUI:
case Instruction::FPToSI:
CSF.DX11_1_DoubleExtensions = true;
break;
}
}
if (!CSF.LowPrecisionPresent)
CSF.LowPrecisionPresent =
I.getType()->isIntegerTy(16) || I.getType()->isHalfTy();
if (!CSF.LowPrecisionPresent) {
for (const Value *Op : I.operands()) {
if (Op->getType()->isIntegerTy(16) || Op->getType()->isHalfTy()) {
CSF.LowPrecisionPresent = true;
break;
}
}
}
if (CSF.LowPrecisionPresent) {
if (CSF.NativeLowPrecisionMode)
CSF.NativeLowPrecision = true;
else
CSF.MinimumPrecision = true;
}
if (!CSF.Int64Ops)
CSF.Int64Ops = I.getType()->isIntegerTy(64);
if (!CSF.Int64Ops && !isa<LifetimeIntrinsic>(&I)) {
for (const Value *Op : I.operands()) {
if (Op->getType()->isIntegerTy(64)) {
CSF.Int64Ops = true;
break;
}
}
}
if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
switch (II->getIntrinsicID()) {
default:
break;
case Intrinsic::dx_resource_handlefrombinding: {
dxil::ResourceTypeInfo &RTI = DRTM[cast<TargetExtType>(II->getType())];
// Set ResMayNotAlias if DXIL validator version >= 1.8 and the function
// uses UAVs
if (!CSF.ResMayNotAlias && CanSetResMayNotAlias &&
MMDI.ValidatorVersion >= VersionTuple(1, 8) && RTI.isUAV())
CSF.ResMayNotAlias = true;
switch (RTI.getResourceKind()) {
case dxil::ResourceKind::StructuredBuffer:
case dxil::ResourceKind::RawBuffer:
CSF.EnableRawAndStructuredBuffers = true;
break;
default:
break;
}
break;
}
case Intrinsic::dx_resource_load_typedbuffer: {
dxil::ResourceTypeInfo &RTI =
DRTM[cast<TargetExtType>(II->getArgOperand(0)->getType())];
if (RTI.isTyped())
CSF.TypedUAVLoadAdditionalFormats |= RTI.getTyped().ElementCount > 1;
break;
}
}
}
// Handle call instructions
if (auto *CI = dyn_cast<CallInst>(&I)) {
const Function *CF = CI->getCalledFunction();
// Merge-in shader flags mask of the called function in the current module
if (FunctionFlags.contains(CF))
CSF.merge(FunctionFlags[CF]);
// TODO: Set DX11_1_DoubleExtensions if I is a call to DXIL intrinsic
// DXIL::Opcode::Fma https://github.com/llvm/llvm-project/issues/114554
CSF.WaveOps |= checkWaveOps(CI->getIntrinsicID());
}
}
/// Set shader flags that apply to all functions within the module
ComputedShaderFlags
ModuleShaderFlags::gatherGlobalModuleFlags(const Module &M,
const DXILResourceMap &DRM,
const ModuleMetadataInfo &MMDI) {
ComputedShaderFlags CSF;
// Set DisableOptimizations flag based on the presence of OptimizeNone
// attribute of entry functions.
if (MMDI.EntryPropertyVec.size() > 0) {
CSF.DisableOptimizations = MMDI.EntryPropertyVec[0].Entry->hasFnAttribute(
llvm::Attribute::OptimizeNone);
// Ensure all entry functions have the same optimization attribute
for (const auto &EntryFunProps : MMDI.EntryPropertyVec)
if (CSF.DisableOptimizations !=
EntryFunProps.Entry->hasFnAttribute(llvm::Attribute::OptimizeNone))
EntryFunProps.Entry->getContext().diagnose(DiagnosticInfoUnsupported(
*(EntryFunProps.Entry), "Inconsistent optnone attribute "));
}
CSF.UAVsAtEveryStage = hasUAVsAtEveryStage(DRM, MMDI);
// Set the Max64UAVs flag if the number of UAVs is > 8
uint32_t NumUAVs = 0;
for (auto &UAV : DRM.uavs())
if (MMDI.ValidatorVersion < VersionTuple(1, 6))
NumUAVs++;
else // MMDI.ValidatorVersion >= VersionTuple(1, 6)
NumUAVs += UAV.getBinding().Size;
if (NumUAVs > 8)
CSF.Max64UAVs = true;
// Set the module flag that enables native low-precision execution mode.
// NativeLowPrecisionMode can only be set when the command line option
// -enable-16bit-types is provided. This is indicated by the dx.nativelowprec
// module flag being set
// This flag is needed even if the module does not use 16-bit types because a
// corresponding debug module may include 16-bit types, and tools that use the
// debug module may expect it to have the same flags as the original
if (auto *NativeLowPrec = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("dx.nativelowprec")))
if (MMDI.ShaderModelVersion >= VersionTuple(6, 2))
CSF.NativeLowPrecisionMode = NativeLowPrec->getValue().getBoolValue();
// Set ResMayNotAlias to true if DXIL validator version < 1.8 and there
// are UAVs present globally.
if (CanSetResMayNotAlias && MMDI.ValidatorVersion < VersionTuple(1, 8))
CSF.ResMayNotAlias = !DRM.uavs().empty();
return CSF;
}
/// Construct ModuleShaderFlags for module Module M
void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
const DXILResourceMap &DRM,
const ModuleMetadataInfo &MMDI) {
CanSetResMayNotAlias = MMDI.DXILVersion >= VersionTuple(1, 7);
// The command line option -res-may-alias will set the dx.resmayalias module
// flag to 1, thereby disabling the ability to set the ResMayNotAlias flag
if (auto *ResMayAlias = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("dx.resmayalias")))
if (ResMayAlias->getValue().getBoolValue())
CanSetResMayNotAlias = false;
ComputedShaderFlags GlobalSFMask = gatherGlobalModuleFlags(M, DRM, MMDI);
CallGraph CG(M);
// Compute Shader Flags Mask for all functions using post-order visit of SCC
// of the call graph.
for (scc_iterator<CallGraph *> SCCI = scc_begin(&CG); !SCCI.isAtEnd();
++SCCI) {
const std::vector<CallGraphNode *> &CurSCC = *SCCI;
// Union of shader masks of all functions in CurSCC
ComputedShaderFlags SCCSF;
// List of functions in CurSCC that are neither external nor declarations
// and hence whose flags are collected
SmallVector<Function *> CurSCCFuncs;
for (CallGraphNode *CGN : CurSCC) {
Function *F = CGN->getFunction();
if (!F)
continue;
if (F->isDeclaration()) {
assert(!F->getName().starts_with("dx.op.") &&
"DXIL Shader Flag analysis should not be run post-lowering.");
continue;
}
ComputedShaderFlags CSF = GlobalSFMask;
for (const auto &BB : *F)
for (const auto &I : BB)
updateFunctionFlags(CSF, I, DRTM, MMDI);
// Update combined shader flags mask for all functions in this SCC
SCCSF.merge(CSF);
CurSCCFuncs.push_back(F);
}
// Update combined shader flags mask for all functions of the module
CombinedSFMask.merge(SCCSF);
// Shader flags mask of each of the functions in an SCC of the call graph is
// the union of all functions in the SCC. Update shader flags masks of
// functions in CurSCC accordingly. This is trivially true if SCC contains
// one function.
for (Function *F : CurSCCFuncs)
// Merge SCCSF with that of F
FunctionFlags[F].merge(SCCSF);
}
}
void ComputedShaderFlags::print(raw_ostream &OS) const {
uint64_t FlagVal = (uint64_t) * this;
OS << formatv("; Shader Flags Value: {0:x8}\n;\n", FlagVal);
if (FlagVal == 0)
return;
OS << "; Note: shader requires additional functionality:\n";
#define SHADER_FEATURE_FLAG(FeatureBit, DxilModuleNum, FlagName, Str) \
if (FlagName) \
(OS << ";").indent(7) << Str << "\n";
#include "llvm/BinaryFormat/DXContainerConstants.def"
OS << "; Note: extra DXIL module flags:\n";
#define DXIL_MODULE_FLAG(DxilModuleBit, FlagName, Str) \
if (FlagName) \
(OS << ";").indent(7) << Str << "\n";
#include "llvm/BinaryFormat/DXContainerConstants.def"
OS << ";\n";
}
/// Return the shader flags mask of the specified function Func.
const ComputedShaderFlags &
ModuleShaderFlags::getFunctionFlags(const Function *Func) const {
auto Iter = FunctionFlags.find(Func);
assert((Iter != FunctionFlags.end() && Iter->first == Func) &&
"Get Shader Flags : No Shader Flags Mask exists for function");
return Iter->second;
}
//===----------------------------------------------------------------------===//
// ShaderFlagsAnalysis and ShaderFlagsAnalysisPrinterPass
// Provide an explicit template instantiation for the static ID.
AnalysisKey ShaderFlagsAnalysis::Key;
ModuleShaderFlags ShaderFlagsAnalysis::run(Module &M,
ModuleAnalysisManager &AM) {
DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(M);
DXILResourceMap &DRM = AM.getResult<DXILResourceAnalysis>(M);
const ModuleMetadataInfo MMDI = AM.getResult<DXILMetadataAnalysis>(M);
ModuleShaderFlags MSFI;
MSFI.initialize(M, DRTM, DRM, MMDI);
return MSFI;
}
PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
ModuleAnalysisManager &AM) {
const ModuleShaderFlags &FlagsInfo = AM.getResult<ShaderFlagsAnalysis>(M);
// Print description of combined shader flags for all module functions
OS << "; Combined Shader Flags for Module\n";
FlagsInfo.getCombinedFlags().print(OS);
// Print shader flags mask for each of the module functions
OS << "; Shader Flags for Module Functions\n";
for (const auto &F : M.getFunctionList()) {
if (F.isDeclaration())
continue;
const ComputedShaderFlags &SFMask = FlagsInfo.getFunctionFlags(&F);
OS << formatv("; Function {0} : {1:x8}\n;\n", F.getName(),
(uint64_t)(SFMask));
}
return PreservedAnalyses::all();
}
//===----------------------------------------------------------------------===//
// ShaderFlagsAnalysis and ShaderFlagsAnalysisPrinterPass
bool ShaderFlagsAnalysisWrapper::runOnModule(Module &M) {
DXILResourceTypeMap &DRTM =
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
DXILResourceMap &DRM =
getAnalysis<DXILResourceWrapperPass>().getResourceMap();
const ModuleMetadataInfo MMDI =
getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
MSFI.initialize(M, DRTM, DRM, MMDI);
return false;
}
void ShaderFlagsAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequiredTransitive<DXILResourceTypeWrapperPass>();
AU.addRequiredTransitive<DXILResourceWrapperPass>();
AU.addRequired<DXILMetadataAnalysisWrapperPass>();
}
char ShaderFlagsAnalysisWrapper::ID = 0;
INITIALIZE_PASS_BEGIN(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
"DXIL Shader Flag Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
INITIALIZE_PASS_END(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
"DXIL Shader Flag Analysis", true, true)
|