1 //===----- CGHLSLRuntime.cpp - Interface to HLSL Runtimes -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This provides an abstract class for HLSL code generation.  Concrete
10 // subclasses of this implement code generation for specific HLSL
11 // runtime libraries.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "CGHLSLRuntime.h"
16 #include "CodeGenModule.h"
17 #include "clang/Basic/TargetOptions.h"
18 #include "llvm/IR/Metadata.h"
19 #include "llvm/IR/Module.h"
20 
21 using namespace clang;
22 using namespace CodeGen;
23 using namespace llvm;
24 
25 namespace {
26 void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
27   // The validation of ValVersionStr is done at HLSLToolChain::TranslateArgs.
28   // Assume ValVersionStr is legal here.
29   VersionTuple Version;
30   if (Version.tryParse(ValVersionStr) || Version.getBuild() ||
31       Version.getSubminor() || !Version.getMinor()) {
32     return;
33   }
34 
35   uint64_t Major = Version.getMajor();
36   uint64_t Minor = *Version.getMinor();
37 
38   auto &Ctx = M.getContext();
39   IRBuilder<> B(M.getContext());
40   MDNode *Val = MDNode::get(Ctx, {ConstantAsMetadata::get(B.getInt32(Major)),
41                                   ConstantAsMetadata::get(B.getInt32(Minor))});
42   StringRef DxilValKey = "dx.valver";
43   M.addModuleFlag(llvm::Module::ModFlagBehavior::AppendUnique, DxilValKey, Val);
44 }
45 } // namespace
46 
47 void CGHLSLRuntime::finishCodeGen() {
48   auto &TargetOpts = CGM.getTarget().getTargetOpts();
49 
50   llvm::Module &M = CGM.getModule();
51   addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
52 }
53