173417c51Spython3kgae //===----- CGHLSLRuntime.cpp - Interface to HLSL Runtimes -----------------===//
273417c51Spython3kgae //
373417c51Spython3kgae // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
473417c51Spython3kgae // See https://llvm.org/LICENSE.txt for license information.
573417c51Spython3kgae // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
673417c51Spython3kgae //
773417c51Spython3kgae //===----------------------------------------------------------------------===//
873417c51Spython3kgae //
973417c51Spython3kgae // This provides an abstract class for HLSL code generation.  Concrete
1073417c51Spython3kgae // subclasses of this implement code generation for specific HLSL
1173417c51Spython3kgae // runtime libraries.
1273417c51Spython3kgae //
1373417c51Spython3kgae //===----------------------------------------------------------------------===//
1473417c51Spython3kgae 
1573417c51Spython3kgae #include "CGHLSLRuntime.h"
1673417c51Spython3kgae #include "CodeGenModule.h"
1773417c51Spython3kgae #include "clang/Basic/TargetOptions.h"
1873417c51Spython3kgae #include "llvm/IR/Metadata.h"
1973417c51Spython3kgae #include "llvm/IR/Module.h"
2073417c51Spython3kgae 
2173417c51Spython3kgae using namespace clang;
2273417c51Spython3kgae using namespace CodeGen;
2373417c51Spython3kgae using namespace llvm;
2473417c51Spython3kgae 
2573417c51Spython3kgae namespace {
addDxilValVersion(StringRef ValVersionStr,llvm::Module & M)2673417c51Spython3kgae void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
2773417c51Spython3kgae   // The validation of ValVersionStr is done at HLSLToolChain::TranslateArgs.
2873417c51Spython3kgae   // Assume ValVersionStr is legal here.
2973417c51Spython3kgae   VersionTuple Version;
3073417c51Spython3kgae   if (Version.tryParse(ValVersionStr) || Version.getBuild() ||
3173417c51Spython3kgae       Version.getSubminor() || !Version.getMinor()) {
3273417c51Spython3kgae     return;
3373417c51Spython3kgae   }
3473417c51Spython3kgae 
3573417c51Spython3kgae   uint64_t Major = Version.getMajor();
36*ca4af13eSKazu Hirata   uint64_t Minor = *Version.getMinor();
3773417c51Spython3kgae 
3873417c51Spython3kgae   auto &Ctx = M.getContext();
3973417c51Spython3kgae   IRBuilder<> B(M.getContext());
4073417c51Spython3kgae   MDNode *Val = MDNode::get(Ctx, {ConstantAsMetadata::get(B.getInt32(Major)),
4173417c51Spython3kgae                                   ConstantAsMetadata::get(B.getInt32(Minor))});
4273417c51Spython3kgae   StringRef DxilValKey = "dx.valver";
4373417c51Spython3kgae   M.addModuleFlag(llvm::Module::ModFlagBehavior::AppendUnique, DxilValKey, Val);
4473417c51Spython3kgae }
4573417c51Spython3kgae } // namespace
4673417c51Spython3kgae 
finishCodeGen()4773417c51Spython3kgae void CGHLSLRuntime::finishCodeGen() {
4873417c51Spython3kgae   auto &TargetOpts = CGM.getTarget().getTargetOpts();
4973417c51Spython3kgae 
5073417c51Spython3kgae   llvm::Module &M = CGM.getModule();
5173417c51Spython3kgae   addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
5273417c51Spython3kgae }
53