Skip to content

Commit 9d095ed

Browse files
committed
tower: update tower api to enable run passes on core:module and enable location transform
1 parent 202c344 commit 9d095ed

File tree

4 files changed

+263
-248
lines changed

4 files changed

+263
-248
lines changed

include/vast/CodeGen/CodeGenDriver.hpp

+17-20
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ VAST_UNRELAX_WARNINGS
1414
#include "vast/Dialect/HighLevel/HighLevelDialect.hpp"
1515
#include "vast/Dialect/HighLevel/HighLevelOps.hpp"
1616

17-
#include "vast/CodeGen/ScopeContext.hpp"
1817
#include "vast/CodeGen/CodeGenModule.hpp"
18+
#include "vast/CodeGen/ScopeContext.hpp"
1919

2020
#include "vast/Frontend/Options.hpp"
2121

@@ -26,43 +26,39 @@ namespace vast::cg {
2626

2727
std::unique_ptr< codegen_builder > mk_codegen_builder(mcontext_t &mctx);
2828

29-
std::shared_ptr< meta_generator > mk_meta_generator(
30-
acontext_t &actx, mcontext_t &mctx, const cc::vast_args &vargs
31-
);
29+
std::shared_ptr< meta_generator >
30+
mk_meta_generator(acontext_t &actx, mcontext_t &mctx, const cc::vast_args &vargs);
3231

33-
std::shared_ptr< symbol_generator > mk_symbol_generator(
34-
acontext_t &actx, mcontext_t &mctx, const cc::vast_args &vargs
35-
);
32+
std::shared_ptr< symbol_generator >
33+
mk_symbol_generator(acontext_t &actx, mcontext_t &mctx, const cc::vast_args &vargs);
3634

3735
std::unique_ptr< mcontext_t > mk_mcontext();
3836

3937
void set_target_triple(core::module mod, std::string triple);
4038
void set_source_language(core::module mod, cc::source_language lang);
4139

42-
owning_mlir_module_ref mk_wrapping_module(mcontext_t &mctx);
40+
owning_mlir_module_ref mk_wrapping_module(acontext_t &actx, mcontext_t &mctx);
4341

4442
core::module mk_module(acontext_t &actx, mlir_module top);
45-
core::module mk_module_with_attrs(acontext_t &actx, mlir_module top, cc::source_language lang);
43+
core::module
44+
mk_module_with_attrs(acontext_t &actx, mlir_module top, cc::source_language lang);
4645

4746
struct driver
4847
{
4948
explicit driver(
50-
acontext_t &_actx
51-
, mcontext_t &_mctx
52-
, std::unique_ptr< codegen_builder > _bld
53-
, std::shared_ptr< visitor_base > _visitor
49+
acontext_t &_actx, mcontext_t &_mctx, std::unique_ptr< codegen_builder > _bld,
50+
std::shared_ptr< visitor_base > _visitor
5451
)
5552
: actx(_actx)
5653
, mctx(_mctx)
5754
, bld(std::move(_bld))
5855
, visitor(std::move(_visitor))
59-
, top(mk_wrapping_module(mctx))
56+
, top(mk_wrapping_module(actx, mctx))
6057
, mod(mk_module_with_attrs(
61-
actx, top.get(), cc::get_source_language(actx.getLangOpts())
62-
))
58+
actx, top.get(), cc::get_source_language(actx.getLangOpts())
59+
))
6360
, scope(symbols)
64-
, generator(*bld, scoped_visitor_view(*visitor, scope))
65-
{
61+
, generator(*bld, scoped_visitor_view(*visitor, scope)) {
6662
bld->module = mod;
6763
bld->set_insertion_point_to_start(&mod.getBodyRegion());
6864
}
@@ -80,6 +76,7 @@ namespace vast::cg {
8076
owning_mlir_module_ref freeze();
8177

8278
mcontext_t &mcontext() { return mctx; }
79+
8380
acontext_t &acontext() { return actx; }
8481

8582
virtual bool verify();
@@ -115,6 +112,6 @@ namespace vast::cg {
115112
};
116113

117114
std::unique_ptr< driver > mk_default_driver(
118-
cc::action_options &opts, const cc::vast_args &vargs,
119-
acontext_t &actx, mcontext_t &mctx);
115+
cc::action_options &opts, const cc::vast_args &vargs, acontext_t &actx, mcontext_t &mctx
116+
);
120117
} // namespace vast::cg

lib/vast/CodeGen/CodeGenDriver.cpp

+23-28
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ namespace vast::cg {
3434

3535
owning_mlir_module_ref driver::freeze() { return std::move(top); }
3636

37-
// TODO this should not be needed the data layout should be emitted from cached types directly
38-
dl::DataLayoutBlueprint emit_data_layout_blueprint(
39-
const acontext_t &actx, const type_caching_proxy &types
40-
) {
37+
// TODO this should not be needed the data layout should be emitted from cached types
38+
// directly
39+
dl::DataLayoutBlueprint
40+
emit_data_layout_blueprint(const acontext_t &actx, const type_caching_proxy &types) {
4141
dl::DataLayoutBlueprint dl;
4242

43-
auto store_layout = [&] (const clang_type *orig, mlir_type vast_type) {
43+
auto store_layout = [&](const clang_type *orig, mlir_type vast_type) {
4444
if (orig->isFunctionType()) {
4545
return;
4646
}
@@ -83,7 +83,9 @@ namespace vast::cg {
8383
auto list = std::dynamic_pointer_cast< visitor_list >(visitor);
8484
for (auto node = list->head; node; node = node->next) {
8585
if (auto types = std::dynamic_pointer_cast< type_caching_proxy >(node)) {
86-
::vast::cg::emit_data_layout(mctx, mod, emit_data_layout_blueprint(actx, *types));
86+
::vast::cg::emit_data_layout(
87+
mctx, mod, emit_data_layout_blueprint(actx, *types)
88+
);
8789
}
8890
}
8991
}
@@ -94,9 +96,8 @@ namespace vast::cg {
9496
return std::make_unique< codegen_builder >(&mctx);
9597
}
9698

97-
std::shared_ptr< meta_generator > mk_meta_generator(
98-
acontext_t *actx, mcontext_t *mctx, const cc::vast_args &vargs
99-
) {
99+
std::shared_ptr< meta_generator >
100+
mk_meta_generator(acontext_t *actx, mcontext_t *mctx, const cc::vast_args &vargs) {
100101
if (vargs.has_option(cc::opt::locs_as_meta_ids)) {
101102
return std::make_shared< id_meta_gen >(actx, mctx);
102103
}
@@ -123,28 +124,23 @@ namespace vast::cg {
123124
// setup visitor list
124125
const bool enable_unsupported = !vargs.has_option(cc::opt::disable_unsupported);
125126

126-
auto mg = mk_meta_generator(&actx, &mctx, vargs);
127+
auto mg = mk_meta_generator(&actx, &mctx, vargs);
127128
auto invalid_mg = mk_invalid_meta_generator(&mctx);
128-
auto sg = mk_symbol_generator(actx);
129-
auto policy = mk_codegen_policy(opts);
129+
auto sg = mk_symbol_generator(actx);
130+
auto policy = mk_codegen_policy(opts);
130131

131132
auto visitors = std::make_shared< visitor_list >()
132-
| as_node_with_list_ref< attr_visitor_proxy >()
133-
| as_node< type_caching_proxy >()
133+
| as_node_with_list_ref< attr_visitor_proxy >() | as_node< type_caching_proxy >()
134134
| as_node_with_list_ref< default_visitor >(
135-
mctx, actx, *bld, std::move(mg), std::move(sg), std::move(policy)
135+
mctx, actx, *bld, std::move(mg), std::move(sg), std::move(policy)
136136
)
137137
| optional(enable_unsupported,
138-
as_node_with_list_ref< unsup_visitor >(
139-
mctx, *bld, std::move(invalid_mg)
140-
)
138+
as_node_with_list_ref< unsup_visitor >(mctx, *bld, std::move(invalid_mg))
141139
)
142140
| as_node< unreach_visitor >();
143141

144142
// setup driver
145-
auto drv = std::make_unique< driver >(
146-
actx, mctx, std::move(bld), visitors
147-
);
143+
auto drv = std::make_unique< driver >(actx, mctx, std::move(bld), visitors);
148144

149145
drv->enable_verifier(!vargs.has_option(cc::opt::disable_vast_verifier));
150146
return drv;
@@ -181,24 +177,23 @@ namespace vast::cg {
181177
}
182178
} // namespace detail
183179

184-
owning_mlir_module_ref mk_wrapping_module(mcontext_t &mctx) {
185-
return mlir::ModuleOp::create(mlir::UnknownLoc::get(&mctx));
180+
owning_mlir_module_ref mk_wrapping_module(acontext_t &actx, mcontext_t &mctx) {
181+
auto [loc, _] = detail::module_loc_name(mctx, actx);
182+
return mlir::ModuleOp::create(loc);
186183
}
187184

188185
core::module mk_module(acontext_t &actx, mlir_module top) {
189186
mlir::OpBuilder bld(top);
190187
bld.setInsertionPointToStart(top.getBody());
191188

192189
// TODO use symbol generator
193-
auto mctx = top.getContext();
190+
auto mctx = top.getContext();
194191
auto [loc, name] = detail::module_loc_name(*mctx, actx);
195192
return bld.create< core::module >(loc, name);
196193
}
197194

198-
core::module mk_module_with_attrs(
199-
acontext_t &actx, mlir_module top,
200-
cc::source_language lang
201-
) {
195+
core::module
196+
mk_module_with_attrs(acontext_t &actx, mlir_module top, cc::source_language lang) {
202197
auto mod = mk_module(actx, top);
203198

204199
set_target_triple(mod, actx.getTargetInfo().getTriple().str());

lib/vast/Tower/Tower.cpp

+31-15
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,37 @@ namespace vast::tw {
1212
module_storage &storage;
1313

1414
// Start empty and after each callback add to it.
15-
conversion_passes_t path = {};
15+
conversion_passes_t path = {};
1616
// TODO: Remove.
1717
conversion_path_t str_path = {};
1818

1919
std::vector< handle_t > handles;
2020
link_vector steps;
2121

22-
2322
explicit link_builder(location_info_t &li, module_storage &storage, handle_t root)
2423
: li(li), storage(storage), handles{ root } {}
2524

2625
void runAfterPass(pass_ptr pass, operation op) override {
27-
auto mod = mlir::dyn_cast< mlir_module >(op);
26+
auto current = op;
27+
while (current && !mlir::isa< mlir_module >(current)) {
28+
current = op->getParentOp();
29+
}
30+
auto mod = mlir::dyn_cast< mlir_module >(current);
2831
VAST_CHECK(mod, "Pass inside tower was not run on module!");
2932

3033
// Update locations so each operation now has a unique loc that also
3134
// encodes backlink.
3235
path.emplace_back(pass);
33-
str_path.emplace_back(pass->getArgument().str());
34-
transform_locations(li, str_path, mod);
3536

36-
owning_mlir_module_ref persistent = mlir::dyn_cast< mlir_module >(op->clone());
37+
// Location transformation depends on the command line argument
38+
// of passes. If it is empty, don't perform location transformation.
39+
if (!pass->getArgument().empty()) {
40+
str_path.emplace_back(pass->getArgument().str());
41+
transform_locations(li, str_path, op);
42+
}
43+
44+
// Clone the module to make it persistent
45+
owning_mlir_module_ref persistent = mlir::dyn_cast< mlir_module >(mod->clone());
3746

3847
auto from = handles.back();
3948
handles.emplace_back(storage.store(path, std::move(persistent)));
@@ -45,11 +54,15 @@ namespace vast::tw {
4554

4655
namespace {
4756

48-
link_vector construct_steps(const std::vector< handle_t > &handles, location_info_t &li) {
57+
link_vector
58+
construct_steps(const std::vector< handle_t > &handles, location_info_t &li) {
4959
VAST_ASSERT(handles.size() >= 2);
5060
link_vector out;
51-
for (std::size_t i = 1; i < handles.size(); ++i)
52-
out.emplace_back(std::make_unique< conversion_step >(handles[i - 1], handles[i], li));
61+
for (std::size_t i = 1; i < handles.size(); ++i) {
62+
out.emplace_back(
63+
std::make_unique< conversion_step >(handles[i - 1], handles[i], li)
64+
);
65+
}
5366
return out;
5467
}
5568

@@ -73,29 +86,32 @@ namespace vast::tw {
7386

7487
link_ptr tower::apply(handle_t root, location_info_t &li, mlir::PassManager &requested_pm) {
7588
std::vector< mlir::Pass * > requested_passes;
76-
for (auto &p : requested_pm.getPasses())
89+
for (auto &p : requested_pm.getPasses()) {
7790
requested_passes.push_back(&p);
91+
}
7892
auto [handles, suffix] = storage.get_maximum_prefix_path(requested_passes, root);
7993

8094
// This path is completely new.
81-
if (handles.empty())
95+
if (handles.empty()) {
8296
return std::make_unique< fat_link >(mk_full_path(root, li, requested_pm));
97+
}
8398

8499
auto as_steps = construct_steps(handles, li);
85100

86101
// This path is already present - construct a link.
87-
if (suffix.empty())
102+
if (suffix.empty()) {
88103
return std::make_unique< fat_link >(std::move(as_steps));
104+
}
89105

90106
auto pm = mlir::PassManager(requested_pm.getContext());
91107
copy_passes(pm, suffix);
92108

93109
auto new_steps = mk_full_path(handles.back(), li, pm);
94110
// TODO: Update with newer stdlib
95111
as_steps.insert(
96-
as_steps.end(),
97-
std::make_move_iterator(new_steps.begin()),
98-
std::make_move_iterator(new_steps.end()));
112+
as_steps.end(), std::make_move_iterator(new_steps.begin()),
113+
std::make_move_iterator(new_steps.end())
114+
);
99115

100116
return std::make_unique< fat_link >(std::move(as_steps));
101117
}

0 commit comments

Comments
 (0)