Skip to content

Commit

Permalink
[SYCL] remove usage of SPIRV library and generate SPIRV binary via ll…
Browse files Browse the repository at this point in the history
…vm-spirv

Signed-off-by: Vladimir Lazarev <vladimir.lazarev@intel.com>
Signed-off-by: Michael D Toguchi <michael.d.toguchi@intel.com>
  • Loading branch information
mdtoguchi authored and vladimirlaz committed Mar 22, 2019
1 parent fbffaab commit ff0f21d
Show file tree
Hide file tree
Showing 22 changed files with 102 additions and 73 deletions.
1 change: 0 additions & 1 deletion clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ LANGOPT(CUDADeviceApproxTranscendentals, 1, 0, "using approximate transcendental
LANGOPT(GPURelocatableDeviceCode, 1, 0, "generate relocatable device code")

LANGOPT(SYCLIsDevice , 1, 0, "Generate code for SYCL device")
LANGOPT(SYCLUseBitcode , 1, 0, "Generate bitcode for SYCL")
LANGOPT(SYCLAllowFuncPtr , 1, 0, "Allow function pointers in SYCL device code")

LANGOPT(SizedDeallocation , 1, 0, "sized deallocation")
Expand Down
1 change: 0 additions & 1 deletion clang/include/clang/CodeGen/BackendUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace clang {
enum BackendAction {
Backend_EmitAssembly, ///< Emit native assembly files
Backend_EmitBC, ///< Emit LLVM bitcode files
Backend_EmitSPIRV, ///< Emit SPIR-V bitcode files
Backend_EmitLL, ///< Emit human-readable LLVM assembly
Backend_EmitNothing, ///< Don't emit anything (benchmarking mode)
Backend_EmitMCNull, ///< Run CodeGen, but don't emit anything
Expand Down
6 changes: 0 additions & 6 deletions clang/include/clang/CodeGen/CodeGenAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ class EmitBCAction : public CodeGenAction {
EmitBCAction(llvm::LLVMContext *_VMContext = nullptr);
};

class EmitSPIRVAction : public CodeGenAction {
virtual void anchor();
public:
EmitSPIRVAction(llvm::LLVMContext *_VMContext = nullptr);
};

class EmitLLVMAction : public CodeGenAction {
virtual void anchor();
public:
Expand Down
14 changes: 13 additions & 1 deletion clang/include/clang/Driver/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ class Action {
OffloadBundlingJobClass,
OffloadUnbundlingJobClass,
OffloadWrappingJobClass,
SPIRVTranslatorJobClass,

JobClassFirst = PreprocessJobClass,
JobClassLast = OffloadWrappingJobClass
JobClassLast = SPIRVTranslatorJobClass
};

// The offloading kind determines if this action is binded to a particular
Expand Down Expand Up @@ -626,6 +627,17 @@ class OffloadWrappingJobAction : public JobAction {
}
};

class SPIRVTranslatorJobAction : public JobAction {
void anchor() override;

public:
SPIRVTranslatorJobAction(Action *Input, types::ID OutputType);

static bool classof(const Action *A) {
return A->getKind() == SPIRVTranslatorJobClass;
}
};

} // namespace driver
} // namespace clang

Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,15 @@ class ToolChain {
mutable std::unique_ptr<Tool> Link;
mutable std::unique_ptr<Tool> OffloadBundler;
mutable std::unique_ptr<Tool> OffloadWrapper;
mutable std::unique_ptr<Tool> SPIRVTranslator;

Tool *getClang() const;
Tool *getAssemble() const;
Tool *getLink() const;
Tool *getClangAs() const;
Tool *getOffloadBundler() const;
Tool *getOffloadWrapper() const;
Tool *getSPIRVTranslator() const;

mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
mutable std::unique_ptr<XRayArgs> XRayArguments;
Expand Down
3 changes: 0 additions & 3 deletions clang/include/clang/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ enum ActionKind {
/// Emit a .bc file.
EmitBC,

/// Emit a .spv file.
EmitSPIRV,

/// Translate input source into HTML.
EmitHTML,

Expand Down
40 changes: 8 additions & 32 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
#include "llvm/SYCL/ASFixer.h"
#include <memory>

#include "LLVMSPIRVLib.h"
namespace SPIRV {
extern llvm::cl::opt<bool> SPIRVNoDerefAttr;
}
Expand Down Expand Up @@ -807,7 +806,6 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,

bool UsesCodeGen = (Action != Backend_EmitNothing &&
Action != Backend_EmitBC &&
Action != Backend_EmitSPIRV &&
Action != Backend_EmitLL);
CreateTargetMachine(UsesCodeGen);

Expand Down Expand Up @@ -841,6 +839,10 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
break;

case Backend_EmitBC:
if (LangOpts.SYCLIsDevice) {
PerModulePasses.add(createASFixerPass());
PerModulePasses.add(createDeadCodeEliminationPass());
}
if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
Expand Down Expand Up @@ -871,24 +873,6 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
}
break;

case Backend_EmitSPIRV:
if (LangOpts.SYCLIsDevice) {
// TODO: SPIRVNoDerefAttr is not modeled when using the bitcode pass
SPIRV::SPIRVNoDerefAttr = true;
// TODO: this pass added to work around missing linkonce_odr in SPIR-V
PerModulePasses.add(
createAlwaysInlinerLegacyPass(true /*InsertLifetimeIntrinsics*/));
PerModulePasses.add(createASFixerPass());
PerModulePasses.add(createDeadCodeEliminationPass());
}
if (LangOpts.SYCLUseBitcode)
PerModulePasses.add(
createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, false));
else
PerModulePasses.add(createSPIRVWriterPass(*OS));

break;

case Backend_EmitLL:
PerModulePasses.add(
createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
Expand Down Expand Up @@ -1203,6 +1187,10 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
break;

case Backend_EmitBC:
if (LangOpts.SYCLIsDevice) {
CodeGenPasses.add(createASFixerPass());
CodeGenPasses.add(createDeadCodeEliminationPass());
}
if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
Expand Down Expand Up @@ -1232,18 +1220,6 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
}
break;

case Backend_EmitSPIRV:
if (LangOpts.SYCLIsDevice) {
SPIRV::SPIRVNoDerefAttr = true;
// TODO: this pass added to work around missing linkonce_odr in SPIR-V
CodeGenPasses.add(
createAlwaysInlinerLegacyPass(true /*InsertLifetimeIntrinsics*/));
CodeGenPasses.add(createASFixerPass());
CodeGenPasses.add(createDeadCodeEliminationPass());
}
CodeGenPasses.add(createSPIRVWriterPass(*OS));
break;

case Backend_EmitLL:
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
break;
Expand Down
1 change: 0 additions & 1 deletion clang/lib/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ set(LLVM_LINK_COMPONENTS
Target
TransformUtils
ASFixer
SPIRVLib
)

# In a standard Clang+LLVM build, we need to generate intrinsics before
Expand Down
6 changes: 0 additions & 6 deletions clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,6 @@ GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) {
return CI.createDefaultOutputFile(false, InFile, "ll");
case Backend_EmitBC:
return CI.createDefaultOutputFile(true, InFile, "bc");
case Backend_EmitSPIRV:
return CI.createDefaultOutputFile(true, InFile, "spv");
case Backend_EmitNothing:
return nullptr;
case Backend_EmitMCNull:
Expand Down Expand Up @@ -1059,10 +1057,6 @@ void EmitBCAction::anchor() { }
EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
: CodeGenAction(Backend_EmitBC, _VMContext) {}

void EmitSPIRVAction::anchor() { }
EmitSPIRVAction::EmitSPIRVAction(llvm::LLVMContext *_VMContext)
: CodeGenAction(Backend_EmitSPIRV, _VMContext) {}

void EmitLLVMAction::anchor() { }
EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
: CodeGenAction(Backend_EmitLL, _VMContext) {}
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Driver/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,9 @@ void OffloadWrappingJobAction::anchor() {}
OffloadWrappingJobAction::OffloadWrappingJobAction(Action *Input,
types::ID Type)
: JobAction(OffloadWrappingJobClass, Input, Type) {}

void SPIRVTranslatorJobAction::anchor() {}

SPIRVTranslatorJobAction::SPIRVTranslatorJobAction(Action *Input,
types::ID Type)
: JobAction(SPIRVTranslatorJobClass, Input, Type) {}
10 changes: 9 additions & 1 deletion clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3853,7 +3853,15 @@ Action *Driver::ConstructPhaseAction(
return C.MakeAction<BackendJobAction>(Input, Output);
}
if (Args.hasArg(options::OPT_sycl)) {
return C.MakeAction<BackendJobAction>(Input, types::TY_SPIRV);
if (Args.hasFlag(options::OPT_fsycl_use_bitcode,
options::OPT_fno_sycl_use_bitcode, true))
return C.MakeAction<BackendJobAction>(Input, types::TY_LLVM_BC);
// Use of --sycl creates a bitcode file, we need to translate that to
// a SPIR-V file with -fno-sycl-use-bitcode
auto *BackendAction =
C.MakeAction<BackendJobAction>(Input, types::TY_LLVM_BC);
return C.MakeAction<SPIRVTranslatorJobAction>(BackendAction,
types::TY_SPIRV);
}
return C.MakeAction<BackendJobAction>(Input, types::TY_PP_Asm);
}
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ Tool *ToolChain::getOffloadWrapper() const {
return OffloadWrapper.get();
}

Tool *ToolChain::getSPIRVTranslator() const {
if (!SPIRVTranslator)
SPIRVTranslator.reset(new tools::SPIRVTranslator(*this));
return SPIRVTranslator.get();
}

Tool *ToolChain::getTool(Action::ActionClass AC) const {
switch (AC) {
case Action::AssembleJobClass:
Expand Down Expand Up @@ -323,6 +329,9 @@ Tool *ToolChain::getTool(Action::ActionClass AC) const {

case Action::OffloadWrappingJobClass:
return getOffloadWrapper();

case Action::SPIRVTranslatorJobClass:
return getSPIRVTranslator();
}

llvm_unreachable("Invalid tool kind.");
Expand Down
35 changes: 28 additions & 7 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3546,10 +3546,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("-aux-triple");
CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
CmdArgs.push_back("-disable-llvm-passes");
if (Args.hasFlag(options::OPT_fsycl_use_bitcode,
options::OPT_fno_sycl_use_bitcode, true)) {
CmdArgs.push_back("-fsycl-use-bitcode");
}
if (Args.hasFlag(options::OPT_fsycl_allow_func_ptr,
options::OPT_fno_sycl_allow_func_ptr, false)) {
CmdArgs.push_back("-fsycl-allow-func-ptr");
Expand Down Expand Up @@ -3600,7 +3596,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}
} else if (isa<AssembleJobAction>(JA)) {
if (IsSYCLOffloadDevice && IsSYCLDevice) {
CmdArgs.push_back("-emit-spirv");
CmdArgs.push_back("-emit-llvm-bc");
} else {
CmdArgs.push_back("-emit-obj");
CollectArgsForIntegratedAssembler(C, Args, CmdArgs, D);
Expand Down Expand Up @@ -3643,8 +3639,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
} else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
CmdArgs.push_back("-rewrite-objc");
rewriteKind = RK_Fragile;
} else if (JA.getType() == types::TY_SPIRV) {
CmdArgs.push_back("-emit-spirv");
} else {
assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!");
}
Expand Down Expand Up @@ -6430,3 +6424,30 @@ void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA,
C.addCommand(llvm::make_unique<Command>(JA, *this, Llc, LlcArgs, None));
}

// Begin SPIRVTranslator

void SPIRVTranslator::ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output,
const InputInfoList &Inputs,
const llvm::opt::ArgList &TCArgs,
const char *LinkingOutput) const {
// Construct llvm-spirv command.
assert(isa<SPIRVTranslatorJobAction>(JA) && "Expecting Translator job!");

// The translator command looks like this:
// llvm-spirv -o <file>.spv <file>.bc
ArgStringList TranslatorArgs;

TranslatorArgs.push_back("-o");
TranslatorArgs.push_back(Output.getFilename());
if (getToolChain().getTriple().isSYCLDeviceEnvironment())
TranslatorArgs.push_back("-spirv-no-deref-attr");
for (auto I : Inputs) {
TranslatorArgs.push_back(I.getFilename());
}

C.addCommand(llvm::make_unique<Command>(JA, *this,
TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
TranslatorArgs, None));
}

13 changes: 13 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ class LLVM_LIBRARY_VISIBILITY OffloadWrapper final : public Tool {
const llvm::opt::ArgList &TCArgs,
const char *LinkingOutput) const override;
};

/// SPIR-V translator tool.
class LLVM_LIBRARY_VISIBILITY SPIRVTranslator final : public Tool {
public:
SPIRVTranslator(const ToolChain &TC)
: Tool("SPIR-V translator", "llvm-spirv", TC) {}

bool hasIntegratedCPP() const override { return false; }
void ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output, const InputInfoList &Inputs,
const llvm::opt::ArgList &TCArgs,
const char *LinkingOutput) const override;
};
} // end namespace tools

} // end namespace driver
Expand Down
7 changes: 0 additions & 7 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1190,8 +1190,6 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
}
}

Opts.EmitOpenCLArgMetadata |= Args.hasArg(OPT_emit_spirv);

if (Arg *A = Args.getLastArg(OPT_fdenormal_fp_math_EQ)) {
StringRef Val = A->getValue();
if (Val == "ieee")
Expand Down Expand Up @@ -1619,8 +1617,6 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
Opts.ProgramAction = frontend::EmitAssembly; break;
case OPT_emit_llvm_bc:
Opts.ProgramAction = frontend::EmitBC; break;
case OPT_emit_spirv:
Opts.ProgramAction = frontend::EmitSPIRV; break;
case OPT_emit_html:
Opts.ProgramAction = frontend::EmitHTML; break;
case OPT_emit_llvm:
Expand Down Expand Up @@ -2891,8 +2887,6 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
}

Opts.SYCLIsDevice = Args.hasArg(options::OPT_fsycl_is_device);
Opts.SYCLUseBitcode = Args.hasFlag(options::OPT_fsycl_use_bitcode,
options::OPT_fno_sycl_use_bitcode, false);
Opts.SYCLAllowFuncPtr = Args.hasFlag(options::OPT_fsycl_allow_func_ptr,
options::OPT_fno_sycl_allow_func_ptr, false);

Expand Down Expand Up @@ -3055,7 +3049,6 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) {
case frontend::ASTView:
case frontend::EmitAssembly:
case frontend::EmitBC:
case frontend::EmitSPIRV:
case frontend::EmitHTML:
case frontend::EmitLLVM:
case frontend::EmitLLVMOnly:
Expand Down
1 change: 0 additions & 1 deletion clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ CreateFrontendBaseAction(CompilerInstance &CI) {
case DumpTokens: return llvm::make_unique<DumpTokensAction>();
case EmitAssembly: return llvm::make_unique<EmitAssemblyAction>();
case EmitBC: return llvm::make_unique<EmitBCAction>();
case EmitSPIRV: return llvm::make_unique<EmitSPIRVAction>();
case EmitHTML: return llvm::make_unique<HTMLPrintAction>();
case EmitLLVM: return llvm::make_unique<EmitLLVMAction>();
case EmitLLVMOnly: return llvm::make_unique<EmitLLVMOnlyAction>();
Expand Down
1 change: 1 addition & 0 deletions clang/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ if( NOT CLANG_BUILT_STANDALONE )
llvm-objdump
llvm-profdata
llvm-readobj
llvm-spirv
llvm-symbolizer
opt
)
Expand Down
3 changes: 2 additions & 1 deletion clang/test/CodeGenSPIRV/intel/is_valid_event.cl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// Builtins must be declared as overloadable, so Clang mangles their names,
// and LLVM-SPIRV translator can recognize them.

// RUN: %clang_cc1 %s -emit-spirv -triple spir-unknown-unknown -O0 -cl-std=CL2.0 -include opencl-c.h -o %t.spv
// RUN: %clang_cc1 %s -emit-llvm-bc -triple spir-unknown-unknown -O0 -cl-std=CL2.0 -include opencl-c.h -o %t.bc
// RUN: llvm-spirv %t.bc -o %t.spv
// RUN: llvm-spirv -to-text %t.spv -o - | FileCheck %s

// CHECK: CreateUserEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %clang_cc1 %s -triple spir-unknown-unknown -O0 -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-LLVM
// RUN: %clang_cc1 %s -triple spir-unknown-unknown -O0 -emit-spirv -o %t.spv
// RUN: %clang_cc1 %s -triple spir-unknown-unknown -O0 -emit-llvm-bc -o %t.bc
// RUN: llvm-spirv %t.bc -o %t.spv
// RUN: llvm-spirv %t.spv -to-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV

// CHECK-LLVM: @__const.test.arr = private unnamed_addr addrspace(2) constant [3 x i32] [i32 1, i32 2, i32 3], align 4
Expand Down
Loading

0 comments on commit ff0f21d

Please sign in to comment.