Skip to content
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

Implement llvm-foreach tool #793

Merged
merged 7 commits into from
Nov 21, 2019
Merged

Conversation

Fznamznon
Copy link
Contributor

@Fznamznon Fznamznon commented Nov 6, 2019

The tool has simple functionality: execute specified command for each
file in input file list.
The tool will be used in the clang driver for cases when number of
action's outputs is unknown and used tools don't support file lists.

Signed-off-by: Mariya Podchishchaeva mariya.podchishchaeva@intel.com

Copy link
Contributor

@asavonic asavonic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please state the motivation for this patch in the commit message (and in the PR description).

The tool looks good overall, and seems to do its job. However, the its command line interface is totally different from *nix xargs. I think that the tool should either be renamed (llvm-foreach?), or (probably better) its interface should be aligned with xargs.


static cl::opt<std::string> OutReplace{
"out-replace",
cl::desc("Replace R in command with temporary filename for writing outputs "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just by looking at the help message, I cannot figure out what this option does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to improve.

llvm/tools/llvm-xargs/llvm-xargs.cpp Outdated Show resolved Hide resolved
llvm/tools/llvm-xargs/llvm-xargs.cpp Outdated Show resolved Hide resolved
@keryell
Copy link
Contributor

keryell commented Nov 7, 2019

It is not clear why you need to reimplement xargs if it is just like xargs...
Just install xargs on the system? Recompile it on Windows? Use WSL? Use a .bat script doing the same?

@Fznamznon
Copy link
Contributor Author

Fznamznon commented Nov 8, 2019

It is not clear why you need to reimplement xargs if it is just like xargs...
Just install xargs on the system? Recompile it on Windows? Use WSL? Use a .bat script doing the same?

The idea was taken from unix xargs but I'm not sure it's exactly xargs. It's xargs with additional functionality - generation of output file list. I think, I'm going to rename it to something like "llvm-foreach" as @asavonic suggests...

Actually it's needed for SYCL device code split feature #631 .
Basically split action will have multiple outputs and number of these outputs is unknown. All these outputs should be passed through some part of SYCL toolchain by the clang driver. The problem is in clang driver. When number of outputs is unknown before compilation invocation, the clang driver can operate only with file lists. But a lot of compilation tools don't have support for file lists, so we are implementing a new tool which will execute other tools for each file from specified list.
So, this tool (llvm-foreach?) will be used in the clang driver to execute tools which don't have support for multiple inputs/filelists. So, to make this tool working with the clang driver on any system, we just implement it.
I'll add this information to commit message soon.

@Fznamznon
Copy link
Contributor Author

Applied comments.
Also I added support of multiple file lists and arguments containing "=".

llvm/tools/llvm-foreach/llvm-foreach.cpp Outdated Show resolved Hide resolved
llvm/tools/llvm-foreach/llvm-foreach.cpp Outdated Show resolved Hide resolved
llvm/tools/llvm-foreach/llvm-foreach.cpp Outdated Show resolved Hide resolved
llvm/tools/llvm-foreach/llvm-foreach.cpp Outdated Show resolved Hide resolved
llvm/tools/llvm-foreach/llvm-foreach.cpp Outdated Show resolved Hide resolved
llvm/tools/llvm-foreach/llvm-foreach.cpp Show resolved Hide resolved
ArgumentReplace OutReplaceArg;
for (size_t i = 0; i < Args.size(); ++i) {
for (auto &Replace : Replaces)
if (Args[i].contains(Replace)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Search for the same substring in the same string happens 3 times: here in contains, in Arg.find and then in Arg.find_last_of.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it's 1 time.


struct ArgumentReplace {
int ArgNum = -1;
std::string Prefix;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is enough to just store an index where Replace string starts, and it's length.
No need to split a string into two allocations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, done.

llvm/tools/llvm-foreach/llvm-foreach.cpp Show resolved Hide resolved
int NumOfLines = 0;
for (; !LineIterators[i].is_at_eof(); ++LineIterators[i]) {
FileLists[i].push_back(LineIterators[i]->str());
NumOfLines++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NumOfLines seems to be the same as FileLists[i].size().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Removed NumOfLines.

Args[OutReplaceArg.ArgNum] = ResOutArg;

if (!OutputFileList.empty())
ResFileList = (Twine(ResFileList) + Twine(Path) + Twine("\n")).str();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to keep concatenating file names into a string, you can just write them to OutputFileList file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, done.

The tool has simple functionality: execute specified command for each
file in input file list.
The tool will be used in the clang driver for cases when number of
action's outputs is unknown and used tools don't support file lists.

Signed-off-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
Signed-off-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
Signed-off-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
Signed-off-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
Signed-off-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
Signed-off-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
Signed-off-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
Copy link
Contributor

@asavonic asavonic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear: I strongly believe that the clang driver is supposed to handle this use case, and making a tool to cover this particular deficiency is a workaround.
Having said that, I've no major objections to the code itself.

@Fznamznon
Copy link
Contributor Author

Just to be clear: I strongly believe that the clang driver is supposed to handle this use case, and making a tool to cover this particular deficiency is a workaround.
Having said that, I've no major objections to the code itself.

Yes, It's some kind of workaround now. But I'm afraid that to support such use cases clang driver needs some significant changes, probably redesign. As you can see, not only we faced such problems with the driver - triSYCL/sycl#73 .
So, I think we can maybe try to start a talk with llvm community about it.

@Fznamznon
Copy link
Contributor Author

Ping.

@romanovvlad romanovvlad merged commit 0543397 into intel:sycl Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants