Skip to content

[SYCL] Implement specialization constants. #1356

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

Merged
merged 9 commits into from
Mar 27, 2020
Merged
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
52 changes: 51 additions & 1 deletion clang/include/clang/Driver/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include <initializer_list>
#include <string>

namespace llvm {
Expand Down Expand Up @@ -78,9 +79,10 @@ class Action {
SYCLPostLinkJobClass,
PartialLinkJobClass,
BackendCompileJobClass,
FileTableTformJobClass,

JobClassFirst = PreprocessJobClass,
JobClassLast = BackendCompileJobClass
JobClassLast = FileTableTformJobClass
};

// The offloading kind determines if this action is binded to a particular
Expand Down Expand Up @@ -679,6 +681,13 @@ class SYCLPostLinkJobAction : public JobAction {
static bool classof(const Action *A) {
return A->getKind() == SYCLPostLinkJobClass;
}

void setRTSetsSpecConstants(bool Val) { RTSetsSpecConsts = Val; }

bool getRTSetsSpecConstants() const { return RTSetsSpecConsts; }

private:
bool RTSetsSpecConsts = true;
};

class PartialLinkJobAction : public JobAction {
Expand All @@ -705,6 +714,47 @@ class BackendCompileJobAction : public JobAction {
}
};

// Represents a file table transformation action. The order of inputs to a
// FileTableTformJobAction at construction time must accord with the tforms
// added later - some tforms "consume" inputs. For example, "replace column"
// needs another file to read the replacement column from.
class FileTableTformJobAction : public JobAction {
void anchor() override;

public:
struct Tform {
enum Kind { EXTRACT, EXTRACT_DROP_TITLE, REPLACE };

Tform() = default;
Tform(Kind K, std::initializer_list<StringRef> Args) : TheKind(K) {
for (auto A : Args)
TheArgs.emplace_back(A.str());
}

Kind TheKind;
SmallVector<std::string, 2> TheArgs;
};

FileTableTformJobAction(Action *Input, types::ID OutputType);
FileTableTformJobAction(ActionList &Inputs, types::ID OutputType);

// Deletes all columns except the one with given name.
void addExtractColumnTform(StringRef ColumnName, bool WithColTitle = true);

// Replaces a column with title <From> in this table with a column with title
// <To> from another file table passed as input to this action.
void addReplaceColumnTform(StringRef From, StringRef To);

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

const ArrayRef<Tform> getTforms() const { return Tforms; }

private:
SmallVector<Tform, 2> Tforms; // transformation actions requested
};

} // 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 @@ -147,6 +147,7 @@ class ToolChain {
mutable std::unique_ptr<Tool> SYCLPostLink;
mutable std::unique_ptr<Tool> PartialLink;
mutable std::unique_ptr<Tool> BackendCompiler;
mutable std::unique_ptr<Tool> FileTableTform;

Tool *getClang() const;
Tool *getFlang() const;
Expand All @@ -161,6 +162,7 @@ class ToolChain {
Tool *getSYCLPostLink() const;
Tool *getPartialLink() const;
Tool *getBackendCompiler() const;
Tool *getTableTform() const;

mutable std::unique_ptr<SanitizerArgs> SanitizerArguments;
mutable std::unique_ptr<XRayArgs> XRayArguments;
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/Types.def
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ TYPE("spirv", SPIRV, INVALID, "spv", phases
TYPE("sycl-header", SYCL_Header, INVALID, "h", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("sycl-fatbin", SYCL_FATBIN, INVALID, nullptr, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("tempfilelist", Tempfilelist, INVALID, "txt", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("tempentriesfilelist", TempEntriesfilelist, INVALID, "txt", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("tempfiletable", Tempfiletable,INVALID, "table", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("tempAOCOfilelist", TempAOCOfilelist, INVALID, "txt", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("archive", Archive, INVALID, "a", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
TYPE("wholearchive", WholeArchive, INVALID, "a", phases::Compile, phases::Backend, phases::Assemble, phases::Link)
Expand Down
10 changes: 10 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ class SYCLIntegrationHeader {
/// invocation descriptor has finished.
void endKernel();

/// Registers a specialization constant to emit info for it into the header.
void addSpecConstant(StringRef IDName, QualType IDType);

private:
// Kernel actual parameter descriptor.
struct KernelParamDesc {
Expand Down Expand Up @@ -407,6 +410,13 @@ class SYCLIntegrationHeader {
/// SYCLIntegrationHeader::startKernel
SmallVector<KernelDesc, 4> KernelDescs;

using SpecConstID = std::pair<QualType, std::string>;

/// Keeps specialization constants met in the translation unit. Maps spec
/// constant's ID type to generated unique name. Duplicates are removed at
/// integration header emission time.
llvm::SmallVector<SpecConstID, 4> SpecConsts;

/// Used for emitting diagnostics.
DiagnosticsEngine &Diag;

Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "CGObjCRuntime.h"
#include "CGOpenMPRuntime.h"
#include "CGRecordLayout.h"
#include "CGSYCLRuntime.h"
#include "CodeGenFunction.h"
#include "CodeGenModule.h"
#include "ConstantEmitter.h"
Expand Down
22 changes: 22 additions & 0 deletions clang/lib/Driver/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const char *Action::getClassName(ActionClass AC) {
return "partial-link";
case BackendCompileJobClass:
return "backend-compiler";
case FileTableTformJobClass:
return "file-table-tform";
}

llvm_unreachable("invalid class");
Expand Down Expand Up @@ -473,3 +475,23 @@ BackendCompileJobAction::BackendCompileJobAction(ActionList &Inputs,
BackendCompileJobAction::BackendCompileJobAction(Action *Input,
types::ID Type)
: JobAction(BackendCompileJobClass, Input, Type) {}

void FileTableTformJobAction::anchor() {}

FileTableTformJobAction::FileTableTformJobAction(Action *Input, types::ID Type)
: JobAction(FileTableTformJobClass, Input, Type) {}

FileTableTformJobAction::FileTableTformJobAction(ActionList &Inputs,
types::ID Type)
: JobAction(FileTableTformJobClass, Inputs, Type) {}

void FileTableTformJobAction::addExtractColumnTform(StringRef ColumnName,
bool WithColTitle) {
auto K = WithColTitle ? Tform::EXTRACT : Tform::EXTRACT_DROP_TITLE;
Tforms.emplace_back(Tform(K, {ColumnName}));
}

void FileTableTformJobAction::addReplaceColumnTform(StringRef From,
StringRef To) {
Tforms.emplace_back(Tform(Tform::REPLACE, {From, To}));
}
28 changes: 23 additions & 5 deletions clang/lib/Driver/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "llvm/Option/OptSpecifier.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/SimpleTable.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <fstream>
Expand Down Expand Up @@ -136,12 +137,29 @@ bool Compilation::CleanupFileList(const TempFileList &Files,
// Temporary file lists contain files that need to be cleaned. The
// file containing the information is also removed
if (File.second == types::TY_Tempfilelist ||
File.second == types::TY_TempEntriesfilelist) {
std::ifstream ListFile(File.first);
if (ListFile) {
// These are temporary files and need to be removed.
File.second == types::TY_Tempfiletable) {
// These are temporary files and need to be removed.
bool IsTable = File.second == types::TY_Tempfiletable;

if (IsTable) {
if (llvm::sys::fs::exists(File.first)) {
auto T = llvm::util::SimpleTable::read(File.first);
if (!T) {
Success = false;
continue;
}
std::vector<std::string> TmpFileNames;
T->get()->linearize(TmpFileNames);

for (const auto &TmpFileName : TmpFileNames) {
if (!TmpFileName.empty())
Success &= CleanupFile(TmpFileName.c_str(), IssueErrors);
}
}
} else {
std::ifstream ListFile(File.first);
std::string TmpFileName;
while(std::getline(ListFile, TmpFileName) && !TmpFileName.empty())
while (std::getline(ListFile, TmpFileName) && !TmpFileName.empty())
Success &= CleanupFile(TmpFileName.c_str(), IssueErrors);
}
}
Expand Down
Loading