Skip to content

Add option for validator-version. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: target_profile
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/CodeGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ class CodeGenOptions : public CodeGenOptionsBase {
/// debug info.
std::string DIBugsReportFilePath;

/// The validator version for dxil.
std::string DxilValidatorVersion;

/// The floating-point denormal mode to use.
llvm::DenormalMode FPDenormalMode = llvm::DenormalMode::getIEEE();

Expand Down
9 changes: 9 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -657,4 +657,13 @@ def err_drv_target_variant_invalid : Error<
def err_drv_invalid_directx_shader_module : Error<
"invalid profile : %0">;

def err_drv_invalid_range_dxil_validator_version : Error<
"invalid validator version : %0\n"
"Validator version must be less than or equal to current internal version.">;
def err_drv_invalid_format_dxil_validator_version : Error<
"invalid validator version : %0\n"
"Format of validator version is \"<major>.<minor>\" (ex:\"1.4\").">;
def err_drv_invalid_empty_dxil_validator_version : Error<
"invalid validator version : %0\n"
"If validator major version is 0, minor version must also be 0.">;
}
6 changes: 6 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -6653,6 +6653,7 @@ def _SLASH_ZW : CLJoined<"ZW">;
def dxc_Group : OptionGroup<"<clang-dxc options>">, Flags<[DXCOption]>,
HelpText<"dxc compatibility options">;


class DXCJoinedOrSeparate<string name> : Option<["/", "-"], name,
KIND_JOINED_OR_SEPARATE>, Group<dxc_Group>, Flags<[DXCOption, NoXarchOption]>;

Expand All @@ -6664,6 +6665,11 @@ def dxc_help : Option<["/", "-", "--"], "help", KIND_JOINED>,
def Fo : DXCJoinedOrSeparate<"Fo">, Alias<o>,
HelpText<"Output object file.">;

def dxil_validator_version : Option<["/", "-"], "validator-version", KIND_SEPARATE>,
Group<dxc_Group>, Flags<[DXCOption, NoXarchOption, CC1Option, HelpHidden]>,
HelpText<"Override validator version for module. Format: <major.minor> ;Default: DXIL.dll version or current internal version.">,
MarshallingInfoString<CodeGenOpts<"DxilValidatorVersion">>;

def target_profile : DXCJoinedOrSeparate<"T">, MetaVarName<"<profile>">,
HelpText<"Set target profile.">,
Values<"ps_6_0, ps_6_1, ps_6_2, ps_6_3, ps_6_4, ps_6_5, ps_6_6, ps_6_7,"
Expand Down
51 changes: 51 additions & 0 deletions clang/lib/CodeGen/CGHLSLRuntime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===----- CGHLSLRuntime.cpp - Interface to HLSL Runtimes -----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This provides an abstract class for HLSL code generation. Concrete
// subclasses of this implement code generation for specific HLSL
// runtime libraries.
//
//===----------------------------------------------------------------------===//

#include "CGHLSLRuntime.h"
#include "CodeGenModule.h"
#include "clang/Basic/CodeGenOptions.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"

using namespace clang;
using namespace CodeGen;
using namespace llvm;

namespace {
void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
// The validation of ValVersionStr is done at HLSLToolChain::TranslateArgs.
// Assume ValVersionStr is legal here.
VersionTuple Version;
if (Version.tryParse(ValVersionStr) || Version.getBuild() ||
Version.getSubminor() || !Version.getMinor()) {
return;
}

uint64_t Major = Version.getMajor();
uint64_t Minor = Version.getMinor().getValue();

auto &Ctx = M.getContext();
IRBuilder<> B(M.getContext());
MDNode *Val = MDNode::get(Ctx, {ConstantAsMetadata::get(B.getInt32(Major)),
ConstantAsMetadata::get(B.getInt32(Minor))});
StringRef DxilValKey = "dx.valver";
M.addModuleFlag(llvm::Module::ModFlagBehavior::AppendUnique, DxilValKey, Val);
}
} // namespace

void CGHLSLRuntime::finishCodeGen() {
auto &CGOpts = CGM.getCodeGenOpts();
llvm::Module &M = CGM.getModule();
addDxilValVersion(CGOpts.DxilValidatorVersion, M);
}
38 changes: 38 additions & 0 deletions clang/lib/CodeGen/CGHLSLRuntime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===----- CGHLSLRuntime.h - Interface to HLSL Runtimes -----*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This provides an abstract class for HLSL code generation. Concrete
// subclasses of this implement code generation for specific HLSL
// runtime libraries.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
#define LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H

namespace clang {

namespace CodeGen {

class CodeGenModule;

class CGHLSLRuntime {
protected:
CodeGenModule &CGM;

public:
CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}
virtual ~CGHLSLRuntime() {}

void finishCodeGen();
};

} // namespace CodeGen
} // namespace clang

#endif
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ add_clang_library(clangCodeGen
CGExprConstant.cpp
CGExprScalar.cpp
CGGPUBuiltin.cpp
CGHLSLRuntime.cpp
CGLoopInfo.cpp
CGNonTrivialStruct.cpp
CGObjC.cpp
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "CGCXXABI.h"
#include "CGCall.h"
#include "CGDebugInfo.h"
#include "CGHLSLRuntime.h"
#include "CGObjCRuntime.h"
#include "CGOpenCLRuntime.h"
#include "CGOpenMPRuntime.h"
Expand Down Expand Up @@ -146,6 +147,8 @@ CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
createOpenMPRuntime();
if (LangOpts.CUDA)
createCUDARuntime();
if (LangOpts.HLSL)
createHLSLRuntime();

// Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
Expand Down Expand Up @@ -262,6 +265,10 @@ void CodeGenModule::createCUDARuntime() {
CUDARuntime.reset(CreateNVCUDARuntime(*this));
}

void CodeGenModule::createHLSLRuntime() {
HLSLRuntime.reset(new CGHLSLRuntime(*this));
}

void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
Replacements[Name] = C;
}
Expand Down Expand Up @@ -805,6 +812,11 @@ void CodeGenModule::Release() {
}
}

// HLSL related end of code gen work items.
if (LangOpts.HLSL) {
getHLSLRuntime().finishCodeGen();
}

if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
assert(PLevel < 3 && "Invalid PIC Level");
getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class CGObjCRuntime;
class CGOpenCLRuntime;
class CGOpenMPRuntime;
class CGCUDARuntime;
class CGHLSLRuntime;
class CoverageMappingModuleGen;
class TargetCodeGenInfo;

Expand Down Expand Up @@ -319,6 +320,7 @@ class CodeGenModule : public CodeGenTypeCache {
std::unique_ptr<CGOpenCLRuntime> OpenCLRuntime;
std::unique_ptr<CGOpenMPRuntime> OpenMPRuntime;
std::unique_ptr<CGCUDARuntime> CUDARuntime;
std::unique_ptr<CGHLSLRuntime> HLSLRuntime;
std::unique_ptr<CGDebugInfo> DebugInfo;
std::unique_ptr<ObjCEntrypoints> ObjCData;
llvm::MDNode *NoObjCARCExceptionsMetadata = nullptr;
Expand Down Expand Up @@ -511,6 +513,7 @@ class CodeGenModule : public CodeGenTypeCache {
void createOpenCLRuntime();
void createOpenMPRuntime();
void createCUDARuntime();
void createHLSLRuntime();

bool isTriviallyRecursive(const FunctionDecl *F);
bool shouldEmitFunction(GlobalDecl GD);
Expand Down Expand Up @@ -609,6 +612,12 @@ class CodeGenModule : public CodeGenTypeCache {
return *CUDARuntime;
}

/// Return a reference to the configured HLSL runtime.
CGHLSLRuntime &getHLSLRuntime() {
assert(HLSLRuntime != nullptr);
return *HLSLRuntime;
}

ObjCEntrypoints &getObjCEntrypoints() const {
assert(ObjCData != nullptr);
return *ObjCData;
Expand Down
15 changes: 15 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3454,6 +3454,16 @@ static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs,
}
}

static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
types::ID InputType) {
const unsigned ForwardedArguments[] = {options::OPT_dxil_validator_version};

for (const auto &Arg : ForwardedArguments)
if (const auto *A = Args.getLastArg(Arg)) {
A->renderAsInput(Args, CmdArgs);
}
}

static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args,
ArgStringList &CmdArgs) {
bool ARCMTEnabled = false;
Expand Down Expand Up @@ -6232,6 +6242,11 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
// Forward -cl options to -cc1
RenderOpenCLOptions(Args, CmdArgs, InputType);

if (C.getDriver().IsDXCMode()) {
// Forward hlsl options to -cc1
RenderHLSLOptions(Args, CmdArgs, InputType);
}

if (IsHIP) {
if (Args.hasFlag(options::OPT_fhip_new_launch_api,
options::OPT_fno_hip_new_launch_api, true))
Expand Down
55 changes: 54 additions & 1 deletion clang/lib/Driver/ToolChains/HLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ using namespace llvm;
namespace {

const unsigned OfflineLibMinor = 0xF;
const unsigned MaxShaderModel6Minor = 7;
const unsigned MaxDXILMajor = 1;
const unsigned MaxDXILMinor = 7;
const unsigned MaxShaderModel6Minor = MaxDXILMinor;
// TODO:get default validator version from validator.
const StringRef DefaultValidatorVer = "1.7";

bool isLegalVersion(VersionTuple Version, unsigned Major, unsigned MinMinor,
unsigned MaxMinor) {
Expand Down Expand Up @@ -122,6 +126,28 @@ std::string tryParseProfile(StringRef Profile) {
return "";
}

bool isLegalValidatorVersion(StringRef ValVersionStr, const Driver &D) {
VersionTuple Version;
if (Version.tryParse(ValVersionStr) || Version.getBuild() ||
Version.getSubminor() || !Version.getMinor()) {
D.Diag(diag::err_drv_invalid_format_dxil_validator_version)
<< ValVersionStr;
return false;
}

uint64_t Major = Version.getMajor();
uint64_t Minor = Version.getMinor().getValue();
if (Major > MaxDXILMajor || (Major == MaxDXILMajor && Minor > MaxDXILMinor)) {
D.Diag(diag::err_drv_invalid_range_dxil_validator_version) << ValVersionStr;
return false;
}
if (Major == 0 && Minor != 0) {
D.Diag(diag::err_drv_invalid_empty_dxil_validator_version) << ValVersionStr;
return false;
}
return true;
}

} // namespace

/// DirectX Toolchain
Expand All @@ -145,3 +171,30 @@ HLSLToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
return ToolChain::ComputeEffectiveClangTriple(Args, InputType);
}
}

DerivedArgList *
HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
Action::OffloadKind DeviceOffloadKind) const {
DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());

const OptTable &Opts = getDriver().getOpts();

for (Arg *A : Args) {
if (A->getOption().getID() == options::OPT_dxil_validator_version) {
StringRef ValVerStr = A->getValue();
std::string ErrorMsg;
if (!isLegalValidatorVersion(ValVerStr, getDriver())) {
continue;
}
}
DAL->append(A);
}
// Add default validator version if not set.
// TODO: remove this once read validator version from validator.
if (!DAL->hasArg(options::OPT_dxil_validator_version)) {
DAL->AddSeparateArg(nullptr,
Opts.getOption(options::OPT_dxil_validator_version),
DefaultValidatorVer);
}
return DAL;
}
3 changes: 3 additions & 0 deletions clang/lib/Driver/ToolChains/HLSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class LLVM_LIBRARY_VISIBILITY HLSLToolChain : public ToolChain {
}
bool isPICDefaultForced() const override { return false; }

llvm::opt::DerivedArgList *
TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
Action::OffloadKind DeviceOffloadKind) const override;
std::string ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args,
types::ID InputType) const override;
};
Expand Down
10 changes: 10 additions & 0 deletions clang/test/CodeGenHLSL/validator_version.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %clang -cc1 -S -triple dxil-pc-shadermodel6.3-library -S -emit-llvm -xhlsl -validator-version 1.1 -o - %s | FileCheck %s

// CHECK:!"dx.valver", ![[valver:[0-9]+]]}
// CHECK:![[valver]] = !{i32 1, i32 1}

float bar(float a, float b);

float foo(float a, float b) {
return bar(a, b);
}
Loading