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

Function call support #472

Closed
georgemitenkov opened this issue Dec 25, 2020 · 4 comments · Fixed by #477
Closed

Function call support #472

georgemitenkov opened this issue Dec 25, 2020 · 4 comments · Fixed by #477
Assignees
Labels
codegen Code generation backend llvm visitor Visitor implementation

Comments

@georgemitenkov
Copy link
Collaborator

Support function calls in NMODL

  • locally defined functions
  • mathematical built-ins

Question: can we call function external to the current mod file?
Question: does ordering of functions matter? (do we need declarations?)

@georgemitenkov georgemitenkov added visitor Visitor implementation codegen Code generation backend llvm labels Dec 25, 2020
@georgemitenkov georgemitenkov self-assigned this Dec 25, 2020
@pramodk
Copy link
Contributor

pramodk commented Dec 25, 2020

can we call function external to the current mod file?

In theory yes but for what would like to do here, we can assume all functions are defined in the mod file.

does ordering of functions matter? (do we need declarations?)

IIRC, I read somewhere in language reference guide that order of method definitions doesn’t matter in the same modules. So prototype declaration is not required (?)

@georgemitenkov
Copy link
Collaborator Author

georgemitenkov commented Dec 25, 2020

  1. Cool, thanks!
  2. Here I mean if we are allowed to have this in NMODL
FUNCTION bar() {bar = foo()}
FUNCTION foo() {foo = 2}

Because traversing AST first meets bar, we need to create call instruction to foo. We pass foo to builder.CreateCall() as a llvm::Function* variable, that in this case hasn't been created.

If NMODL takes care of these cases already, then we can assume all functions in calls have been processed beforehand.

@pramodk
Copy link
Contributor

pramodk commented Dec 25, 2020

Here I mean if we are allowed to have this in NMODL
FUNCTION bar() {bar = foo()}
FUNCTION foo() {foo = 2}

Understood now! NMODL doesn't handle this i.e. order in the AST is same as how user write. For this reason, C++ code generation backend does emit prototype declarations for all functions and procedure at the beginning.

IIRC, prototype declaration is straightforward in LLVM IR, right? In that case it would be safe to emit them at the beginning.

I suggest two options:

  • if you see issues or complications to implement next option then assume that procedures or functions are in the "right" order and continue implementation.
  • get list of functions + procedures and emit declarations at the beginning. For this, we can use CodegenHelperVisitor. As name suggest, this visitor is used to traverse AST and "collect" necessary information to help code generation passes. It's used in
    CodegenHelperVisitor v;

Basically, in visit_program, we can run CodegenHelperVisitor to populate CodegenInfo structure which collect lot of information including list of functions and procedures:

visit_program(const Program& node) {
    CodegenHelperVisitor v;
    auto& info = v.analyze(node);

    for (const auto& node: info.functions) {
        // emit declaration
    }
    for (const auto& node: info.procedures) {
         // emit declaration
    }
....
}

We do the same in C++ backend here

void CodegenCVisitor::print_function_prototypes() {

@georgemitenkov
Copy link
Collaborator Author

Thanks! Yes, I just wanted to clarify before implementing these. I think that emitting declarations is a good and rather straightforward approach.

georgemitenkov added a commit that referenced this issue Dec 30, 2020
This patch adds support for function call code generation, particularly:

- User-defined procedures and functions can now lowered to LLVM IR.
- A framework for external method calls (e.g. sin, exp, etc.) has been created, currently `exp` and `pow` are supported.
- Corresponding tests added.

fixes #472
@georgemitenkov georgemitenkov linked a pull request Jan 12, 2021 that will close this issue
pramodk pushed a commit that referenced this issue Feb 23, 2021
This patch adds support for function call code generation, particularly:

- User-defined procedures and functions can now lowered to LLVM IR.
- A framework for external method calls (e.g. sin, exp, etc.) has been created, currently `exp` and `pow` are supported.
- Corresponding tests added.

fixes #472
pramodk pushed a commit that referenced this issue May 8, 2021
This patch adds support for function call code generation, particularly:

- User-defined procedures and functions can now lowered to LLVM IR.
- A framework for external method calls (e.g. sin, exp, etc.) has been created, currently `exp` and `pow` are supported.
- Corresponding tests added.

fixes #472
pramodk pushed a commit that referenced this issue Mar 8, 2022
This patch adds support for function call code generation, particularly:

- User-defined procedures and functions can now lowered to LLVM IR.
- A framework for external method calls (e.g. sin, exp, etc.) has been created, currently `exp` and `pow` are supported.
- Corresponding tests added.

fixes #472
iomaganaris pushed a commit that referenced this issue May 10, 2022
This patch adds support for function call code generation, particularly:

- User-defined procedures and functions can now lowered to LLVM IR.
- A framework for external method calls (e.g. sin, exp, etc.) has been created, currently `exp` and `pow` are supported.
- Corresponding tests added.

fixes #472
iomaganaris pushed a commit that referenced this issue May 12, 2022
This patch adds support for function call code generation, particularly:

- User-defined procedures and functions can now lowered to LLVM IR.
- A framework for external method calls (e.g. sin, exp, etc.) has been created, currently `exp` and `pow` are supported.
- Corresponding tests added.

fixes #472
iomaganaris pushed a commit that referenced this issue Sep 15, 2022
This patch adds support for function call code generation, particularly:

- User-defined procedures and functions can now lowered to LLVM IR.
- A framework for external method calls (e.g. sin, exp, etc.) has been created, currently `exp` and `pow` are supported.
- Corresponding tests added.

fixes #472
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codegen Code generation backend llvm visitor Visitor implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants