Skip to content

[SYCL] Support per-object file compilation #7595

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 23 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion clang/include/clang/Driver/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,8 @@ class FileTableTformJobAction : public JobAction {
REPLACE,
REPLACE_CELL,
RENAME,
COPY_SINGLE_FILE
COPY_SINGLE_FILE,
MERGE
};

Tform() = default;
Expand Down Expand Up @@ -855,6 +856,10 @@ class FileTableTformJobAction : public JobAction {
// output file.
void addCopySingleFileTform(StringRef ColumnName, int Row);

// Merges all tables from filename listed at column <ColumnName> into a
// single output table.
void addMergeTform(StringRef ColumnName);

static bool classof(const Action *A) {
return A->getKind() == FileTableTformJobClass;
}
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Driver/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,10 @@ void FileTableTformJobAction::addCopySingleFileTform(StringRef ColumnName,
Tform(Tform::COPY_SINGLE_FILE, {ColumnName, std::to_string(Row)}));
}

void FileTableTformJobAction::addMergeTform(StringRef ColumnName) {
Tforms.emplace_back(Tform(Tform::MERGE, {ColumnName}));
}

void AppendFooterJobAction::anchor() {}

AppendFooterJobAction::AppendFooterJobAction(Action *Input, types::ID Type)
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9842,6 +9842,13 @@ void FileTableTform::ConstructJob(Compilation &C, const JobAction &JA,
addArgs(CmdArgs, TCArgs, {Arg});
break;
}
case FileTableTformJobAction::Tform::MERGE: {
assert(Tf.TheArgs.size() == 1 && "column name expected");
SmallString<128> Arg("-merge=");
Arg += Tf.TheArgs[0];
addArgs(CmdArgs, TCArgs, {Arg});
break;
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Support/SimpleTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class SimpleTable {
Row &operator[](int I) { return Rows[I]; }
const Row &operator[](int I) const { return Rows[I]; }

// Merge another table into this table
Error merge(const SimpleTable &Other);

private:
Error addColumnName(StringRef ColName);
void rebuildName2NumMapping();
Expand Down
14 changes: 14 additions & 0 deletions llvm/lib/Support/SimpleTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,19 @@ Expected<SimpleTable::UPtrTy> SimpleTable::read(const Twine &FileName,
return read(MemBuf->get(), ColSep);
}

Error SimpleTable::merge(const SimpleTable &Other) {
if (getNumColumns() != Other.getNumColumns())
return makeError("different number of columns");
if (ColumnNames != Other.ColumnNames)
return makeError("different column names");
for (unsigned I = 0; I < Other.Rows.size(); I++) {
const auto &OtherRow = Other[I];
SmallVector<StringRef, 3> NewRow(OtherRow.Cells.begin(),
OtherRow.Cells.end());
addRow(std::move(NewRow));
}
return Error::success();
}

} // namespace util
} // namespace llvm
3 changes: 3 additions & 0 deletions llvm/test/tools/file-table-tform/Inputs/a1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[11|12]
44|55
66|77
5 changes: 5 additions & 0 deletions llvm/test/tools/file-table-tform/Inputs/merge-gold.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[11|12]
00|11
22|33
44|55
66|77
3 changes: 3 additions & 0 deletions llvm/test/tools/file-table-tform/Inputs/merge-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[One|Two]
0|a0.txt
1|a1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Merge two tables listed in merge-input.txt
RUN: cp %S/Inputs/a{0,1}.txt .
RUN: file-table-tform --merge=Two %S/Inputs/merge-input.txt -o merge-output.txt

-- Verify result
RUN: diff %S/Inputs/merge-gold.txt merge-output.txt
118 changes: 91 additions & 27 deletions llvm/tools/file-table-tform/file-table-tform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ static constexpr char OPT_REPLACE_CELL[] = "replace_cell";
static constexpr char OPT_RENAME[] = "rename";
static constexpr char OPT_EXTRACT[] = "extract";
static constexpr char OPT_COPY_SINGLE_FILE[] = "copy_single_file";
static constexpr char OPT_MERGE[] = "merge";

static cl::list<std::string> TformReplace{
OPT_REPLACE, cl::ZeroOrMore, cl::desc("replace a column"),
Expand All @@ -107,6 +108,10 @@ static cl::list<std::string> TformCopySingleFile{
cl::value_desc("<column name or ordinal>,<row id ordinal>"),
cl::cat(FileTableTformCat)};

static cl::list<std::string> TformMerge{
OPT_MERGE, cl::Optional, cl::desc("merge all input tables"),
cl::value_desc("<column name or ordinal>"), cl::cat(FileTableTformCat)};

static cl::opt<bool> DropTitles{"drop_titles", cl::Optional,
cl::desc("drop column titles"),
cl::cat(FileTableTformCat)};
Expand Down Expand Up @@ -172,7 +177,8 @@ struct TformCmd {
.Case(OPT_RENAME, [&](TformCmd *Cmd) { return Error::success(); })
.Case(OPT_EXTRACT, [&](TformCmd *Cmd) { return Error::success(); })
.Case(OPT_COPY_SINGLE_FILE,
[&](TformCmd *Cmd) { return Error::success(); });
[&](TformCmd *Cmd) { return Error::success(); })
.Case(OPT_MERGE, [&](TformCmd *Cmd) { return Error::success(); });
return F(this);
}

Expand Down Expand Up @@ -234,15 +240,24 @@ struct TformCmd {
std::back_inserter(Args));
return Error::success();
})
.Case(OPT_COPY_SINGLE_FILE, [&](TformCmd *Cmd) -> Error {
// argument is <column name>,<row index>
.Case(OPT_COPY_SINGLE_FILE,
[&](TformCmd *Cmd) -> Error {
// argument is <column name>,<row index>
if (Arg.empty())
return makeUserError("empty argument in " +
Twine(OPT_COPY_SINGLE_FILE));
Arg.split(Args, ',');
if (Args.size() != 2 || Args[0].empty() || Args[1].empty())
return makeUserError("invalid argument in " +
Twine(OPT_COPY_SINGLE_FILE));
return Error::success();
})
.Case(OPT_MERGE, [&](TformCmd *Cmd) -> Error {
if (Arg.empty())
return makeUserError("empty argument in " +
Twine(OPT_COPY_SINGLE_FILE));
return makeUserError("empty argument in " + Twine(OPT_MERGE));
Arg.split(Args, ',');
if (Args.size() != 2 || Args[0].empty() || Args[1].empty())
return makeUserError("invalid argument in " +
Twine(OPT_COPY_SINGLE_FILE));
if (Args.size() != 1 || Args[0].empty())
return makeUserError("invalid argument in " + Twine(OPT_MERGE));
return Error::success();
});
return F(this);
Expand Down Expand Up @@ -286,23 +301,63 @@ struct TformCmd {
Error Res = Table.peelColumns(Args);
return Res ? std::move(Res) : std::move(Error::success());
})
.Case(OPT_COPY_SINGLE_FILE, [&](TformCmd *Cmd) -> Error {
// argument is <column name>,<row index>
assert(Args.size() == 2);
const int Row = std::stoi(Args[1].str());
if (Row >= Table.getNumRows())
return makeUserError("row index is out of bounds");
.Case(OPT_COPY_SINGLE_FILE,
[&](TformCmd *Cmd) -> Error {
// argument is <column name>,<row index>
assert(Args.size() == 2);
const int Row = std::stoi(Args[1].str());
if (Row >= Table.getNumRows())
return makeUserError("row index is out of bounds");

// Copy the file from the only remaining row at specified
// column
StringRef FileToCopy = Table[Row].getCell(Args[0], "");
// Copy the file from the only remaining row at specified
// column
StringRef FileToCopy = Table[Row].getCell(Args[0], "");

if (FileToCopy.empty())
return makeUserError("no file found in specified column");
if (FileToCopy.empty())
return makeUserError("no file found in specified column");

std::error_code EC = sys::fs::copy_file(FileToCopy, Output);
return EC ? createFileError(Output, EC)
: std::move(Error::success());
})
.Case(OPT_MERGE, [&](TformCmd *Cmd) -> Error {
StringRef FileToCopy = Table[0].getCell(Args[0]);
if (!llvm::sys::fs::exists(FileToCopy))
return makeUserError("first file not found");
std::error_code EC = sys::fs::copy_file(FileToCopy, Output);
return EC ? createFileError(Output, EC)
: std::move(Error::success());
if (EC)
return createFileError(Output, EC);

Expected<util::SimpleTable::UPtrTy> OutputTable =
util::SimpleTable::read(Output);
if (!OutputTable)
return OutputTable.takeError();

// The first table will be the table merged into, so start merging
// from the second table.
for (int Row = 1; Row < Table.getNumRows(); Row++) {
// Each cell in the input table is a path
// to another table.
Expected<util::SimpleTable::UPtrTy> Table1 =
util::SimpleTable::read(Table[Row].getCell(Args[0]));
if (!Table1)
return Table1.takeError();

Error err = (*OutputTable)->merge(*Table1.get());
if (err)
return err;
}
raw_fd_ostream Out{Output, EC, sys::fs::OpenFlags::OF_None};

if (EC)
return createFileError(Output, EC);

(*OutputTable)->write(Out, (*OutputTable)->getNumColumns() > 1);

if (Out.has_error())
return createFileError(Output, Out.error());
Out.close();
return Error::success();
});
return F(this);
}
Expand Down Expand Up @@ -344,9 +399,10 @@ int main(int argc, char **argv) {
// yet, as an order across all command line options-commands needs to be
// established first to properly map inputs to commands.

auto Lists = {std::addressof(TformReplace), std::addressof(TformReplaceCell),
std::addressof(TformRename), std::addressof(TformExtract),
std::addressof(TformCopySingleFile)};
auto Lists = {
std::addressof(TformReplace), std::addressof(TformReplaceCell),
std::addressof(TformRename), std::addressof(TformExtract),
std::addressof(TformCopySingleFile), std::addressof(TformMerge)};

for (const auto *L : Lists) {
for (auto It = L->begin(); It != L->end(); It++) {
Expand All @@ -369,13 +425,21 @@ int main(int argc, char **argv) {

// ensure that if copy_single_file is specified, it must be the last tform
bool HasCopySingleFileTform = false;
bool HasMerge = false;
for (auto &P : Cmds) {
if (HasCopySingleFileTform) {
CHECK_AND_EXIT(
makeUserError("copy_single_file must be the last transformation"));
}
if (HasMerge) {
CHECK_AND_EXIT(makeUserError("merge must be the last transformation"));
}
HasCopySingleFileTform = P.second->Kind == OPT_COPY_SINGLE_FILE;
HasMerge = P.second->Kind == OPT_MERGE;
}
if (HasMerge && HasCopySingleFileTform)
CHECK_AND_EXIT(
makeUserError("copy_single_file and merge cannot be used together"));

for (auto &P : Cmds) {
TformCmd::UPtrTy &Cmd = P.second;
Expand All @@ -397,9 +461,9 @@ int main(int argc, char **argv) {
CHECK_AND_EXIT(std::move(Res));
}

// If copy_single_file was specified the output file is generated by the
// corresponding transformation.
if (!HasCopySingleFileTform) {
// If copy_single_file or merge was specified the output file is generated by
// the corresponding transformation.
if (!HasCopySingleFileTform && !HasMerge) {
// Write the transformed table to file
std::error_code EC;
raw_fd_ostream Out{Output, EC, sys::fs::OpenFlags::OF_None};
Expand Down