Skip to content

Fixing & for C backend #1057

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

Merged
merged 3 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion integration_tests/expr_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
def add(x: i32, y: i32) -> i32:
return x + y

@inline
def and_op(x: i32, y: i32) -> i32:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see. I think this feature isn't available in LLVM backend yet (will confirm though). I will add the same (after confirming).

cc: @certik @ronnuriel @rebcabin

Copy link
Contributor

Choose a reason for hiding this comment

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

Go for it. Tests pass, but I assume LLVM ignores it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well I am wondering if we should use the function inline pass for this. A function will be inlined in LLVM backend if it's decorated with @inline. Make sense?

In --fast flag all functions (provided they satisfy some conditions) are anyways inlined automatically.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes. LLVM ignores this decorator. Making it work via function inline pass.

return x & y

def main0():
x: i32; y: i32
x: i32
y: i32
z: i32
x = (2+3)*5
y = add(x, 2)*2
assert x == 25
assert y == 54

z = and_op(x, y)
assert z == 16


main0()

# Not implemented yet in LPython:
Expand Down
1 change: 1 addition & 0 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,7 @@ R"(#include <stdio.h>
case (ASR::binopType::Sub) : { last_expr_precedence = 6; break; }
case (ASR::binopType::Mul) : { last_expr_precedence = 5; break; }
case (ASR::binopType::Div) : { last_expr_precedence = 5; break; }
case (ASR::binopType::BitAnd) : { last_expr_precedence = 5; break; }
case (ASR::binopType::Pow) : {
src = "pow(" + left + ", " + right + ")";
if (is_c) {
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/asr-expr_01-a0d4829.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-expr_01-a0d4829",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/expr_01.py",
"infile_hash": "d9a85a1c6bc50a17c74af218dcd9ee87047b4c3f3bc54f699810d339",
"infile_hash": "c391a2740682aec6070efdfb508697052e45950589fdabd9728127a4",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr_01-a0d4829.stdout",
"stdout_hash": "3b7c5a7a71d5398c67c630cb06af463e785676b02a2cfb077e290500",
"stdout_hash": "c07cae48da2ca27369c507eacdd4b8635ffc2af555ae48bc7614874c",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-expr_01-a0d4829.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 5 {}) _lpython_main_program [] [] [(SubroutineCall 1 main0 () [] ())] () Source Public Implementation () .false. .false. .false. .false.), add: (Function (SymbolTable 2 {_lpython_return_variable: (Variable 2 _lpython_return_variable ReturnVar () () Default (Integer 4 []) Source Public Required .false.), x: (Variable 2 x In () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 2 y In () () Default (Integer 4 []) Source Public Required .false.)}) add [(Var 2 x) (Var 2 y)] [] [(= (Var 2 _lpython_return_variable) (IntegerBinOp (Var 2 x) Add (Var 2 y) (Integer 4 []) ()) ()) (Return)] (Var 2 _lpython_return_variable) Source Public Implementation () .false. .false. .false. .true.), main0: (Function (SymbolTable 3 {x: (Variable 3 x Local () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 3 y Local () () Default (Integer 4 []) Source Public Required .false.)}) main0 [] [] [(= (Var 3 x) (IntegerBinOp (IntegerBinOp (IntegerConstant 2 (Integer 4 [])) Add (IntegerConstant 3 (Integer 4 [])) (Integer 4 []) (IntegerConstant 5 (Integer 4 []))) Mul (IntegerConstant 5 (Integer 4 [])) (Integer 4 []) (IntegerConstant 25 (Integer 4 []))) ()) (= (Var 3 y) (IntegerBinOp (FunctionCall 1 add () [((Var 3 x)) ((IntegerConstant 2 (Integer 4 [])))] (Integer 4 []) () ()) Mul (IntegerConstant 2 (Integer 4 [])) (Integer 4 []) ()) ()) (Assert (IntegerCompare (Var 3 x) Eq (IntegerConstant 25 (Integer 4 [])) (Logical 4 []) ()) ()) (Assert (IntegerCompare (Var 3 y) Eq (IntegerConstant 54 (Integer 4 [])) (Logical 4 []) ()) ())] () Source Public Implementation () .false. .false. .false. .false.), main_program: (Program (SymbolTable 4 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 6 {}) _lpython_main_program [] [] [(SubroutineCall 1 main0 () [] ())] () Source Public Implementation () .false. .false. .false. .false.), add: (Function (SymbolTable 2 {_lpython_return_variable: (Variable 2 _lpython_return_variable ReturnVar () () Default (Integer 4 []) Source Public Required .false.), x: (Variable 2 x In () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 2 y In () () Default (Integer 4 []) Source Public Required .false.)}) add [(Var 2 x) (Var 2 y)] [] [(= (Var 2 _lpython_return_variable) (IntegerBinOp (Var 2 x) Add (Var 2 y) (Integer 4 []) ()) ()) (Return)] (Var 2 _lpython_return_variable) Source Public Implementation () .false. .false. .false. .true.), and_op: (Function (SymbolTable 3 {_lpython_return_variable: (Variable 3 _lpython_return_variable ReturnVar () () Default (Integer 4 []) Source Public Required .false.), x: (Variable 3 x In () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 3 y In () () Default (Integer 4 []) Source Public Required .false.)}) and_op [(Var 3 x) (Var 3 y)] [] [(= (Var 3 _lpython_return_variable) (IntegerBinOp (Var 3 x) BitAnd (Var 3 y) (Integer 4 []) ()) ()) (Return)] (Var 3 _lpython_return_variable) Source Public Implementation () .false. .false. .false. .true.), main0: (Function (SymbolTable 4 {x: (Variable 4 x Local () () Default (Integer 4 []) Source Public Required .false.), y: (Variable 4 y Local () () Default (Integer 4 []) Source Public Required .false.), z: (Variable 4 z Local () () Default (Integer 4 []) Source Public Required .false.)}) main0 [] [] [(= (Var 4 x) (IntegerBinOp (IntegerBinOp (IntegerConstant 2 (Integer 4 [])) Add (IntegerConstant 3 (Integer 4 [])) (Integer 4 []) (IntegerConstant 5 (Integer 4 []))) Mul (IntegerConstant 5 (Integer 4 [])) (Integer 4 []) (IntegerConstant 25 (Integer 4 []))) ()) (= (Var 4 y) (IntegerBinOp (FunctionCall 1 add () [((Var 4 x)) ((IntegerConstant 2 (Integer 4 [])))] (Integer 4 []) () ()) Mul (IntegerConstant 2 (Integer 4 [])) (Integer 4 []) ()) ()) (Assert (IntegerCompare (Var 4 x) Eq (IntegerConstant 25 (Integer 4 [])) (Logical 4 []) ()) ()) (Assert (IntegerCompare (Var 4 y) Eq (IntegerConstant 54 (Integer 4 [])) (Logical 4 []) ()) ()) (= (Var 4 z) (FunctionCall 1 and_op () [((Var 4 x)) ((Var 4 y))] (Integer 4 []) () ()) ()) (Assert (IntegerCompare (Var 4 z) Eq (IntegerConstant 16 (Integer 4 [])) (Logical 4 []) ()) ())] () Source Public Implementation () .false. .false. .false. .false.), main_program: (Program (SymbolTable 5 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])
4 changes: 2 additions & 2 deletions tests/reference/c-expr_01-28f449f.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "c-expr_01-28f449f",
"cmd": "lpython --no-color --show-c {infile}",
"infile": "tests/../integration_tests/expr_01.py",
"infile_hash": "d9a85a1c6bc50a17c74af218dcd9ee87047b4c3f3bc54f699810d339",
"infile_hash": "c391a2740682aec6070efdfb508697052e45950589fdabd9728127a4",
"outfile": null,
"outfile_hash": null,
"stdout": "c-expr_01-28f449f.stdout",
"stdout_hash": "a6972cc7e2bcbfe51adf6a85e7ced9a87f630b63e0e13db7520866f1",
"stdout_hash": "5a99318a6ea27779a9b7e2608adc497a92f1033ac2fe51365d2dc151",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
11 changes: 11 additions & 0 deletions tests/reference/c-expr_01-28f449f.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct dimension_descriptor
// Forward declarations
void _lpython_main_program();
inline __attribute__((always_inline)) int32_t add(int32_t x, int32_t y);
inline __attribute__((always_inline)) int32_t and_op(int32_t x, int32_t y);
void main0();

// Implementations
Expand All @@ -52,14 +53,24 @@ inline __attribute__((always_inline)) int32_t add(int32_t x, int32_t y)
return _lpython_return_variable;
}

inline __attribute__((always_inline)) int32_t and_op(int32_t x, int32_t y)
{
int32_t _lpython_return_variable;
_lpython_return_variable = x&y;
return _lpython_return_variable;
}

void main0()
{
int32_t x;
int32_t y;
int32_t z;
x = (2 + 3)*5;
y = add(x, 2)*2;
ASSERT(x == 25);
ASSERT(y == 54);
z = and_op(x, y);
ASSERT(z == 16);
}

int main(int argc, char* argv[])
Expand Down