Skip to content

Allow message printing with assert in the LLVM backend #384

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 2 commits into from
Apr 17, 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
11 changes: 9 additions & 2 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3150,8 +3150,15 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
start_new_block(elseBB);

{
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("Assertion failed\n");
printf(context, *module, *builder, {fmt_ptr});
if (x.m_msg) {
char* s = ASR::down_cast<ASR::ConstantString_t>(x.m_msg)->m_s;
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("AssertionError: %s\n");
llvm::Value *fmt_ptr2 = builder->CreateGlobalStringPtr(s);
printf(context, *module, *builder, {fmt_ptr, fmt_ptr2});
} else {
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("AssertionError\n");
printf(context, *module, *builder, {fmt_ptr});
}
int exit_code_int = 1;
llvm::Value *exit_code = llvm::ConstantInt::get(context,
llvm::APInt(32, exit_code_int));
Expand Down
13 changes: 13 additions & 0 deletions tests/reference/llvm-assert1-8df4f31.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "llvm-assert1-8df4f31",
"cmd": "lpython --no-color --show-llvm {infile} -o {outfile}",
"infile": "tests/assert1.py",
"infile_hash": "0ff84ea5ccd3d0815cbb66e1c3d3acd61dee7257c98aa1a27ceeef3b",
"outfile": null,
"outfile_hash": null,
"stdout": "llvm-assert1-8df4f31.stdout",
"stdout_hash": "43ba1e8f621d033901f9c161f1962b78a7861d5fc2abc225602f326c",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
51 changes: 51 additions & 0 deletions tests/reference/llvm-assert1-8df4f31.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
; ModuleID = 'LFortran'
source_filename = "LFortran"

@0 = private unnamed_addr constant [20 x i8] c"AssertionError: %s\0A\00", align 1
@1 = private unnamed_addr constant [11 x i8] c"a is not 5\00", align 1
@2 = private unnamed_addr constant [16 x i8] c"AssertionError\0A\00", align 1

define void @test_assert() {
.entry:
%a = alloca i32, align 4
store i32 5, i32* %a, align 4
%0 = load i32, i32* %a, align 4
%1 = icmp eq i32 %0, 5
br i1 %1, label %then, label %else

then: ; preds = %.entry
br label %ifcont

else: ; preds = %.entry
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @0, i32 0, i32 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @1, i32 0, i32 0))
call void @exit(i32 1)
br label %ifcont

ifcont: ; preds = %else, %then
%2 = load i32, i32* %a, align 4
%3 = icmp ne i32 %2, 10
br i1 %3, label %then1, label %else2

then1: ; preds = %ifcont
br label %ifcont3

else2: ; preds = %ifcont
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @2, i32 0, i32 0))
call void @exit(i32 1)
br label %ifcont3

ifcont3: ; preds = %else2, %then1
br label %return

return: ; preds = %ifcont3
ret void
}

declare void @_lfortran_printf(i8*, ...)

declare void @exit(i32)

define i32 @main() {
.entry:
ret i32 0
}
1 change: 1 addition & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ filename = "assert1.py"
ast = true
asr = true
cpp = true
llvm = true

[[test]]
filename = "assign1.py"
Expand Down