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

[refactor] Remove FrontendContext and the global context #911

Closed
wants to merge 1 commit into from

Conversation

k-ye
Copy link
Member

@k-ye k-ye commented May 2, 2020

Since Program is global, and it also provides current_kernel, I think current_ast_builder() can be implemented via current_program -> current_kernel -> ir_builder. This PR inlines FrontendContext' members directly into Kernel, and removes the global FrontendContext context.

@yuanming-hu i saw that FrontendContext was introduced for quite some time now (Feb 2019). Could you take a look and confirm if this change makes sense? I think current_ast_builder() is used when constructing the AST, during which time program.current_kernel is set:

program.current_kernel = this;
program.start_function_definition(this);
func();
program.end_function_definition();
program.current_kernel = nullptr;

Related issue = #689

[Click here for the format server]

@k-ye k-ye requested a review from yuanming-hu May 2, 2020 10:51
@yuanming-hu
Copy link
Member

So here are my thoughts (not sure it makes sense though)

  • We are trying to decouple the frontend (taichi lang) from the middle end (CHI)
  • To decouple we may want to construct a frontend AST without a Program or Kernel. Then we use lower_ast and pass the lowered CHI IR to a Kernel.
  • This means having a FrontendContext for AST creation might be helpful?
  • get_current_program() and Program::current_kernel should actually be removed in the future. These are global states that we should avoid. (Sorry about my bad initial design...) In fact, I already find Program::current_kernel painful since I'm adding parallel compilation. Then multiple threads may fight for this variable.

WDYT?

@k-ye
Copy link
Member Author

k-ye commented May 2, 2020

Can't agree more..

To decouple we may want to construct a frontend AST without a Program or Kernel. Then we use lower_ast and pass the lowered CHI IR to a Kernel.

Ah I see. Currently I see that each Kernel resets FrontendContext, and thought it's coupled.

This means having a FrontendContext for AST creation might be helpful?

Yeah, but I'd still hope FrontendContext not to be global, especially considering that parallel compilation implies parallel AST building? Do you have an idea that, if FrontendContext is decoupled from Kernel, who would own it? Maybe there would be a collection of FrontendContext inside Program or something?

@k-ye k-ye marked this pull request as draft May 2, 2020 23:39
@yuanming-hu
Copy link
Member

Yeah, but I'd still hope FrontendContext not to be global, especially considering that parallel compilation implies parallel AST building? Do you have an idea that, if FrontendContext is decoupled from Kernel, who would own it? Maybe there would be a collection of FrontendContext inside Program or something?

Right, we should not make FrontendContext global. In fact, I think it should be named TaichiASTBuilder (which combines the current FrontendContext and IRBuilder), and the method who is creating the AST owns it. After the AST is built and fetched, the builder should be safe to delete. It should be similar to the design of llvm::IRBuilder.

@k-ye
Copy link
Member Author

k-ye commented May 2, 2020

and the method who is creating the AST owns it.

Right, the problem is that many IRs are implicitly using current_ast_builder, which is global.. (This is also exported to python for some Stmts, see

m.def("create_internal_func_stmt", [&](const std::string &msg) {
current_ast_builder().insert(std::make_unique<InternalFuncStmt>(msg));
});
)

I guess there would have to be a major refactor around how we build AST... i.e. instead of IR stmts know where to find that builder, we inverse the dependency and put the IR stmt into a builder?

@k-ye k-ye removed the request for review from yuanming-hu May 2, 2020 23:50
@yuanming-hu
Copy link
Member

we inverse the dependency and put the IR stmt into a builder?

Now sure if I correctly get your point, but here is a potential solution: simply make create_internal_func_stmt a member of TaichiASTBuilder, so that we can do something like
ast_builder.create_internal_func_stmt(...). Then this would be the ast_builder we need. (Maybe you mean the same thing...)

@k-ye
Copy link
Member Author

k-ye commented May 3, 2020

Ha yeah, that is what I meant... Let me close this PR

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.

2 participants