Skip to content

Commit a63bdc7

Browse files
authored
Merge pull request #384 from namannimmo10/llvm_assert
Allow message printing with `assert` in the LLVM backend
2 parents 3feef19 + c1e0205 commit a63bdc7

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,8 +3150,15 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
31503150
start_new_block(elseBB);
31513151

31523152
{
3153-
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("Assertion failed\n");
3154-
printf(context, *module, *builder, {fmt_ptr});
3153+
if (x.m_msg) {
3154+
char* s = ASR::down_cast<ASR::ConstantString_t>(x.m_msg)->m_s;
3155+
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("AssertionError: %s\n");
3156+
llvm::Value *fmt_ptr2 = builder->CreateGlobalStringPtr(s);
3157+
printf(context, *module, *builder, {fmt_ptr, fmt_ptr2});
3158+
} else {
3159+
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("AssertionError\n");
3160+
printf(context, *module, *builder, {fmt_ptr});
3161+
}
31553162
int exit_code_int = 1;
31563163
llvm::Value *exit_code = llvm::ConstantInt::get(context,
31573164
llvm::APInt(32, exit_code_int));
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "llvm-assert1-8df4f31",
3+
"cmd": "lpython --no-color --show-llvm {infile} -o {outfile}",
4+
"infile": "tests/assert1.py",
5+
"infile_hash": "0ff84ea5ccd3d0815cbb66e1c3d3acd61dee7257c98aa1a27ceeef3b",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "llvm-assert1-8df4f31.stdout",
9+
"stdout_hash": "43ba1e8f621d033901f9c161f1962b78a7861d5fc2abc225602f326c",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; ModuleID = 'LFortran'
2+
source_filename = "LFortran"
3+
4+
@0 = private unnamed_addr constant [20 x i8] c"AssertionError: %s\0A\00", align 1
5+
@1 = private unnamed_addr constant [11 x i8] c"a is not 5\00", align 1
6+
@2 = private unnamed_addr constant [16 x i8] c"AssertionError\0A\00", align 1
7+
8+
define void @test_assert() {
9+
.entry:
10+
%a = alloca i32, align 4
11+
store i32 5, i32* %a, align 4
12+
%0 = load i32, i32* %a, align 4
13+
%1 = icmp eq i32 %0, 5
14+
br i1 %1, label %then, label %else
15+
16+
then: ; preds = %.entry
17+
br label %ifcont
18+
19+
else: ; preds = %.entry
20+
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))
21+
call void @exit(i32 1)
22+
br label %ifcont
23+
24+
ifcont: ; preds = %else, %then
25+
%2 = load i32, i32* %a, align 4
26+
%3 = icmp ne i32 %2, 10
27+
br i1 %3, label %then1, label %else2
28+
29+
then1: ; preds = %ifcont
30+
br label %ifcont3
31+
32+
else2: ; preds = %ifcont
33+
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @2, i32 0, i32 0))
34+
call void @exit(i32 1)
35+
br label %ifcont3
36+
37+
ifcont3: ; preds = %else2, %then1
38+
br label %return
39+
40+
return: ; preds = %ifcont3
41+
ret void
42+
}
43+
44+
declare void @_lfortran_printf(i8*, ...)
45+
46+
declare void @exit(i32)
47+
48+
define i32 @main() {
49+
.entry:
50+
ret i32 0
51+
}

tests/tests.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ filename = "assert1.py"
148148
ast = true
149149
asr = true
150150
cpp = true
151+
llvm = true
151152

152153
[[test]]
153154
filename = "assign1.py"

0 commit comments

Comments
 (0)