-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mir: replace custom_target string command with find_program call
This is a pass that can be run once, in the early phase, to replace cases where the first argument to `custom_target` is a string, not a `file()`, `find_program()`, or `executable()`, and transform it into a `find_program()` call. Fixes: #86
- Loading branch information
Showing
7 changed files
with
144 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright © 2024 Intel Corporation | ||
|
||
#include "argument_extractors.hpp" | ||
#include "passes.hpp" | ||
#include "private.hpp" | ||
|
||
namespace MIR::Passes { | ||
|
||
namespace { | ||
|
||
bool program_replacement_impl(Instruction & obj) { | ||
if (!std::holds_alternative<MIR::FunctionCall>(*obj.obj_ptr)) { | ||
return false; | ||
} | ||
|
||
auto & fc = std::get<MIR::FunctionCall>(*obj.obj_ptr); | ||
if (fc.name != "custom_target") { | ||
return false; | ||
} | ||
|
||
const auto & cmd_obj_v = | ||
extract_keyword_argument_v<MIR::Array, MIR::String>(fc.kw_args, "command"); | ||
// TODO: should this be an error? | ||
if (std::holds_alternative<std::monostate>(cmd_obj_v)) { | ||
return false; | ||
} | ||
|
||
if (std::holds_alternative<MIR::Array>(cmd_obj_v)) { | ||
auto commands = std::get<MIR::Array>(cmd_obj_v).value; | ||
// TODO: should this be an error? | ||
if (commands.empty()) { | ||
return false; | ||
} | ||
const Instruction & cmd = commands.at(0); | ||
if (std::holds_alternative<MIR::String>(*cmd.obj_ptr)) { | ||
auto s = std::get<MIR::String>(*cmd.obj_ptr); | ||
MIR::FunctionCall fp{"find_program", {std::move(s)}, fc.source_dir}; | ||
commands[0] = std::move(fp); | ||
fc.kw_args["command"] = Array{std::move(commands)}; | ||
} | ||
} else if (std::holds_alternative<MIR::String>(cmd_obj_v)) { | ||
auto s = std::get<MIR::String>(cmd_obj_v); | ||
MIR::FunctionCall fp{"find_program", {std::move(s)}, fc.source_dir}; | ||
fc.kw_args["command"] = Array{{std::move(fp)}}; | ||
} else { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace | ||
|
||
bool custom_target_program_replacement(BasicBlock & block) { | ||
return instruction_walker(block, {program_replacement_impl}); | ||
} | ||
|
||
} // namespace MIR::Passes |
65 changes: 65 additions & 0 deletions
65
src/mir/passes/tests/custom_target_program_replacement_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright © 2024 Intel Corporation | ||
|
||
#include "passes.hpp" | ||
#include "passes/argument_extractors.hpp" | ||
#include "passes/private.hpp" | ||
#include "test_utils.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(custom_target_program_replacement, string) { | ||
auto irlist = lower(R"EOF( | ||
x = custom_target('name', command : 'foo.py') | ||
)EOF"); | ||
|
||
MIR::Passes::block_walker(irlist, {MIR::Passes::custom_target_program_replacement}); | ||
|
||
ASSERT_EQ(irlist.instructions.size(), 1); | ||
|
||
const auto & fc_obj = irlist.instructions.front(); | ||
const auto & fc = std::get<MIR::FunctionCall>(*fc_obj.obj_ptr); | ||
|
||
const auto & commands_o = | ||
MIR::Passes::extract_keyword_argument<MIR::Array>(fc.kw_args, "command"); | ||
ASSERT_TRUE(commands_o.has_value()); | ||
|
||
const auto & commands = commands_o.value(); | ||
ASSERT_EQ(commands.value.size(), 1); | ||
|
||
const auto & cmd0 = std::get<MIR::FunctionCall>(*commands.value.at(0).obj_ptr); | ||
EXPECT_EQ(cmd0.name, "find_program"); | ||
|
||
ASSERT_EQ(cmd0.pos_args.size(), 1); | ||
const auto & arg_o = MIR::Passes::extract_positional_argument<MIR::String>(cmd0.pos_args.at(0)); | ||
ASSERT_TRUE(arg_o.has_value()); | ||
ASSERT_EQ(arg_o.value().value, "foo.py"); | ||
} | ||
|
||
TEST(custom_target_program_replacement, array) { | ||
auto irlist = lower(R"EOF( | ||
x = custom_target('name', command : ['foo.py', '@INPUT@', '@OUTPUT@']) | ||
)EOF"); | ||
|
||
MIR::Passes::block_walker(irlist, {MIR::Passes::custom_target_program_replacement}); | ||
|
||
ASSERT_EQ(irlist.instructions.size(), 1); | ||
|
||
const auto & fc_obj = irlist.instructions.front(); | ||
const auto & fc = std::get<MIR::FunctionCall>(*fc_obj.obj_ptr); | ||
|
||
const auto & commands_o = | ||
MIR::Passes::extract_keyword_argument<MIR::Array>(fc.kw_args, "command"); | ||
ASSERT_TRUE(commands_o.has_value()); | ||
|
||
const auto & commands = commands_o.value(); | ||
ASSERT_EQ(commands.value.size(), 3); | ||
|
||
const auto & cmd0 = std::get<MIR::FunctionCall>(*commands.value.at(0).obj_ptr); | ||
EXPECT_EQ(cmd0.name, "find_program"); | ||
|
||
ASSERT_EQ(cmd0.pos_args.size(), 1); | ||
const auto & arg_o = MIR::Passes::extract_positional_argument<MIR::String>(cmd0.pos_args.at(0)); | ||
ASSERT_TRUE(arg_o.has_value()); | ||
ASSERT_EQ(arg_o.value().value, "foo.py"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters