Skip to content

Commit

Permalink
[spirv] Add bare bone code for SPIR-V codegen (#233)
Browse files Browse the repository at this point in the history
* [spirv] Add libclangSPIRV and skeleton FrontendAction for SPIR-V

* [spirv] Add -spirv into dxc for invoking EmitSPIRVAction

* [spirv] Build SPIR-V codegen conditionally

Added ENABLE_SPIRV_CODEGEN in CMake config to control the
building of SPIR-V component and wrap up SPIR-V code using it.
Also add -spirv to hctbuild to turn it on.
  • Loading branch information
antiagainst authored and marcelolr committed May 5, 2017
1 parent 47d21ad commit 2c7a3fc
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ option(HLSL_ENABLE_FIXED_VER "Sets up fixed version information." OFF) # HLSL Ch
option(HLSL_ENABLE_ANALYZE "Enables compiler analysis during compilation." OFF) # HLSL Change
option(HLSL_OPTIONAL_PROJS_IN_DEFAULT "Include optional projects in default build target." OFF) # HLSL Change

# SPIRV change starts
option(ENABLE_SPIRV_CODEGEN "Enables SPIR-V code generation." OFF)
if (${ENABLE_SPIRV_CODEGEN})
add_definitions(-DENABLE_SPIRV_CODEGEN)
endif()
# SPIRV change ends

include(VersionFromVCS)

option(LLVM_APPEND_VC_REV
Expand Down
1 change: 1 addition & 0 deletions include/dxc/Support/HLSLOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class DxcOpts {

bool AllResourcesBound; // OPT_all_resources_bound
bool AstDump; // OPT_ast_dump
bool GenSPIRV; // OPT_spirv // SPIRV change
bool ColorCodeAssembly; // OPT_Cc
bool CodeGenHighLevel; // OPT_fcgl
bool DebugInfo; // OPT__SLASH_Zi
Expand Down
2 changes: 2 additions & 0 deletions include/dxc/Support/HLSLOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ def _help_question : Flag<["-", "/"], "?">, Flags<[DriverOption]>, Alias<help>;

def ast_dump : Flag<["-", "/"], "ast-dump">, Flags<[CoreOption, DriverOption, HelpHidden]>,
HelpText<"Dumps the parsed Abstract Syntax Tree.">; // should not be core, but handy workaround until explicit API written
def spirv : Flag<["-"], "spirv">, Flags<[CoreOption, DriverOption, HelpHidden]>, // SPIRV change: temporary solution to support
HelpText<"Generates SPIR-V binary code">; // SPIRV change: SPIR-V gen in a command line tool
def external_lib : Separate<["-", "/"], "external">, Group<hlslcore_Group>, Flags<[DriverOption, HelpHidden]>,
HelpText<"External DLL name to load for compiler support">;
def external_fn : Separate<["-", "/"], "external-fn">, Group<hlslcore_Group>, Flags<[DriverOption, HelpHidden]>,
Expand Down
1 change: 1 addition & 0 deletions lib/DxcSupport/HLSLOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
opts.UseHexLiterals = Args.hasFlag(OPT_Lx, OPT_INVALID);
opts.Preprocess = Args.getLastArgValue(OPT_P);
opts.AstDump = Args.hasFlag(OPT_ast_dump, OPT_INVALID, false);
opts.GenSPIRV = Args.hasFlag(OPT_spirv, OPT_INVALID, false); // SPIRV change
opts.CodeGenHighLevel = Args.hasFlag(OPT_fcgl, OPT_INVALID, false);
opts.DebugInfo = Args.hasFlag(OPT__SLASH_Zi, OPT_INVALID, false);
opts.VariableName = Args.getLastArgValue(OPT_Vn);
Expand Down
24 changes: 24 additions & 0 deletions tools/clang/include/clang/SPIRV/EmitSPIRVAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- EmitSPIRVAction.h - FrontendAction for Emitting SPIR-V --*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_SPIRV_EMITSPIRVACTION_H
#define LLVM_CLANG_SPIRV_EMITSPIRVACTION_H

#include "clang/Frontend/FrontendAction.h"

namespace clang {

class EmitSPIRVAction : public ASTFrontendAction {
protected:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) override;
};

} // end namespace clang

#endif
26 changes: 26 additions & 0 deletions tools/clang/include/clang/SPIRV/SPIRVBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- SPIRVBuilder.h - SPIR-V builder --*- C++ -*------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_SPIRV_SPIRVBUILDER_H
#define LLVM_CLANG_SPIRV_SPIRVBUILDER_H

namespace clang {
namespace spirv {

class SPIRVBuilder {
public:
SPIRVBuilder() : NextID(0) {}

private:
uint32_t NextID;
};

} // end namespace spirv
} // end namespace clang

#endif
5 changes: 5 additions & 0 deletions tools/clang/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ if(CLANG_ENABLE_STATIC_ANALYZER)
add_subdirectory(StaticAnalyzer)
endif()
add_subdirectory(Format)
# SPIRV change starts
if (ENABLE_SPIRV_CODEGEN)
add_subdirectory(SPIRV)
endif (ENABLE_SPIRV_CODEGEN)
# SPIRV change ends
14 changes: 14 additions & 0 deletions tools/clang/lib/SPIRV/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(LLVM_LINK_COMPONENTS
Support
)

add_clang_library(clangSPIRV
EmitSPIRVAction.cpp
SPIRVBuilder.cpp

LINK_LIBS
clangAST
clangBasic
clangFrontend
clangLex
)
43 changes: 43 additions & 0 deletions tools/clang/lib/SPIRV/EmitSPIRVAction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===--- EmitSPIRVAction.cpp - EmitSPIRVAction implementation -------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "clang/SPIRV/EmitSPIRVAction.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"

namespace clang {
namespace {
class SPIRVEmitter : public ASTConsumer,
public RecursiveASTVisitor<SPIRVEmitter> {
public:
explicit SPIRVEmitter(raw_ostream *Out) : OutStream(*Out) {}

void HandleTranslationUnit(ASTContext &Context) override {
OutStream << "SPIR-V";
}

private:
raw_ostream &OutStream;
};
}

std::unique_ptr<ASTConsumer>
EmitSPIRVAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
return llvm::make_unique<SPIRVEmitter>(CI.getOutStream());
}
} // end namespace clang
12 changes: 12 additions & 0 deletions tools/clang/lib/SPIRV/SPIRVBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===--- SPIRVBuilder.cpp - SPIR-V builder implementation -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

namespace clang {
namespace spirv {} // end namespace spirv
} // end namespace clang
16 changes: 16 additions & 0 deletions tools/clang/tools/dxc/dxc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,22 @@ int __cdecl wmain(int argc, const wchar_t **argv_) {
return 0;
}

// SPIRV change starts
#ifdef ENABLE_SPIRV_CODEGEN
// This ideally should be put in ReadDxcOpts(), but ReadDxcOpts() are called
// again in DxcCompilerCompile() with -Fo stripped.
if (dxcOpts.GenSPIRV && dxcOpts.OutputObject.empty()) {
fprintf(stderr, "-spirv requires -Fo for output object file name.");
return 1;
}
#else
if (dxcOpts.GenSPIRV) {
fprintf(stderr, "SPIR-V codegen not configured via ENABLE_SPIRV_CODEGEN");
return 1;
}
#endif
// SPIRV change ends

// Apply defaults.
if (dxcOpts.EntryPoint.empty() && !dxcOpts.RecompileFromBinary) {
dxcOpts.EntryPoint = "main";
Expand Down
5 changes: 5 additions & 0 deletions tools/clang/tools/dxcompiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ set(GENERATED_HEADERS

add_clang_library(dxcompiler SHARED ${SOURCES})
target_link_libraries(dxcompiler PRIVATE ${LIBRARIES} ${DIASDK_LIBRARIES})
# SPIRV change starts
if (ENABLE_SPIRV_CODEGEN)
target_link_libraries(dxcompiler PRIVATE clangSPIRV)
endif (ENABLE_SPIRV_CODEGEN)
# SPIRV change ends
add_dependencies(dxcompiler DxcEtw)
include_directories(AFTER ${LLVM_INCLUDE_DIR}/dxc/Tracing ${DIASDK_INCLUDE_DIRS})

Expand Down
17 changes: 17 additions & 0 deletions tools/clang/tools/dxcompiler/dxcompilerobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
#include "dxc/HLSL/DxilPipelineStateValidation.h"
#include "dxc/HLSL/HLSLExtensionsCodegenHelper.h"
#include "dxc/HLSL/DxilRootSignature.h"
// SPIRV change starts
#ifdef ENABLE_SPIRV_CODEGEN
#include "clang/SPIRV/EmitSPIRVAction.h"
#endif
// SPIRV change ends

#if defined(_MSC_VER)
#include <io.h>
Expand Down Expand Up @@ -2209,6 +2214,18 @@ class DxcCompiler : public IDxcCompiler, public IDxcLangExtensions, public IDxcC
IFT(pContainerStream.QueryInterface(&pOutputBlob));
}
}
// SPIRV change starts
#ifdef ENABLE_SPIRV_CODEGEN
else if (opts.GenSPIRV) {
clang::EmitSPIRVAction action;
FrontendInputFile file(utf8SourceName.m_psz, IK_HLSL);
action.BeginSourceFile(compiler, file);
action.Execute();
action.EndSourceFile();
outStream.flush();
}
#endif
// SPIRV change ends
else {
llvm::LLVMContext llvmContext;
EmitBCAction action(&llvmContext);
Expand Down
8 changes: 8 additions & 0 deletions utils/hct/hctbuild.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ if "%BUILD_VS_VER%"=="2015" (
)
)

rem Begin SPIRV change
if "%1"=="-spirv" (
echo SPIR-V codegen is enabled.
set CMAKE_OPTS=%CMAKE_OPTS% -DENABLE_SPIRV_CODEGEN:BOOL=ON
shift /1
)
rem End SPIRV change

if "%BUILD_ARCH%"=="x64" (
set BUILD_GENERATOR=%BUILD_GENERATOR% %BUILD_ARCH:x64=Win64%
)
Expand Down

0 comments on commit 2c7a3fc

Please sign in to comment.