Skip to content

Fix printing of i8 and i16 integers in LLVM #1355

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 6 commits into from
Dec 13, 2022
Merged
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -199,6 +199,7 @@ RUN(NAME exit_02c FAIL LABELS cpython llvm c)
# Test all four backends
RUN(NAME print_01 LABELS cpython llvm c wasm) # wasm not yet supports sep and end keywords
RUN(NAME print_03 LABELS x86 c wasm_x86) # simple test case specifically for x86 and wasm_x86
RUN(NAME print_04 LABELS cpython llvm c)

# CPython and LLVM
RUN(NAME const_01 LABELS cpython llvm c)
10 changes: 10 additions & 0 deletions integration_tests/print_04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from ltypes import i8, i16, i32, i64

u: i64 = i64(-922337203685477580)
print(u)
x: i32 = i32(-2147483648)
print(x)
y: i16 = i16(-32768)
print(y)
z: i8 = i8(-128)
print(z)
4 changes: 2 additions & 2 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
@@ -5642,11 +5642,11 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
} else if (ASRUtils::is_integer(*t)) {
switch( a_kind ) {
case 1 : {
fmt.push_back("%d");
fmt.push_back("%hhi");
break;
}
case 2 : {
fmt.push_back("%d");
fmt.push_back("%hi");
break;
}
case 4 : {
2 changes: 1 addition & 1 deletion tests/reference/llvm-ltypes1-dacf939.json
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "llvm-ltypes1-dacf939.stdout",
"stdout_hash": "6bf240f208e3d2f3c9f0e5173b2b431f84123e56a293373b7327d7af",
"stdout_hash": "b05edd4fb6a315a5f992c3d0b7f5eb2121c4ac38b2bab21d1e0a998b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
8 changes: 4 additions & 4 deletions tests/reference/llvm-ltypes1-dacf939.stdout
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ source_filename = "LFortran"

@0 = private unnamed_addr constant [2 x i8] c" \00", align 1
@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@2 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
@2 = private unnamed_addr constant [6 x i8] c"%hi%s\00", align 1
@3 = private unnamed_addr constant [2 x i8] c" \00", align 1
@4 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@5 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
@@ -12,14 +12,14 @@ source_filename = "LFortran"
@8 = private unnamed_addr constant [7 x i8] c"%lld%s\00", align 1
@9 = private unnamed_addr constant [2 x i8] c" \00", align 1
@10 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@11 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
@11 = private unnamed_addr constant [7 x i8] c"%hhi%s\00", align 1

define void @test_i16() {
.entry:
%i = alloca i16, align 2
store i16 4, i16* %i, align 2
%0 = load i16, i16* %i, align 2
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @2, i32 0, i32 0), i16 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0))
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @2, i32 0, i32 0), i16 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0))
br label %return

return: ; preds = %.entry
@@ -55,7 +55,7 @@ define void @test_i8() {
%i = alloca i8, align 1
store i8 5, i8* %i, align 1
%0 = load i8, i8* %i, align 1
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @11, i32 0, i32 0), i8 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @10, i32 0, i32 0))
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @11, i32 0, i32 0), i8 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @10, i32 0, i32 0))
br label %return

return: ; preds = %.entry
13 changes: 13 additions & 0 deletions tests/reference/llvm-print_04-443a8d8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "llvm-print_04-443a8d8",
"cmd": "lpython --no-color --show-llvm {infile} -o {outfile}",
"infile": "tests/../integration_tests/print_04.py",
"infile_hash": "d9eb6feb9aaa52c5cac94b3edd6c62f2611eda2c5ad191e44b42f480",
"outfile": null,
"outfile_hash": null,
"stdout": "llvm-print_04-443a8d8.stdout",
"stdout_hash": "e31293d8330c55c3ec75e1d9b929a70bfabe43a5272a5dbf29c5aadb",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
43 changes: 43 additions & 0 deletions tests/reference/llvm-print_04-443a8d8.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
; ModuleID = 'LFortran'
source_filename = "LFortran"

@u = global i64 -922337203685477580
@x = global i32 -2147483648
@y = global i16 -32768
@z = global i8 -128
@0 = private unnamed_addr constant [2 x i8] c" \00", align 1
@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@2 = private unnamed_addr constant [7 x i8] c"%lld%s\00", align 1
@3 = private unnamed_addr constant [2 x i8] c" \00", align 1
@4 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@5 = private unnamed_addr constant [5 x i8] c"%d%s\00", align 1
@6 = private unnamed_addr constant [2 x i8] c" \00", align 1
@7 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@8 = private unnamed_addr constant [6 x i8] c"%hi%s\00", align 1
@9 = private unnamed_addr constant [2 x i8] c" \00", align 1
@10 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@11 = private unnamed_addr constant [7 x i8] c"%hhi%s\00", align 1

define void @_lpython_main_program() {
.entry:
%0 = load i64, i64* @u, align 4
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @2, i32 0, i32 0), i64 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0))
%1 = load i32, i32* @x, align 4
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @5, i32 0, i32 0), i32 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @4, i32 0, i32 0))
%2 = load i16, i16* @y, align 2
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @8, i32 0, i32 0), i16 %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @7, i32 0, i32 0))
%3 = load i8, i8* @z, align 1
call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @11, i32 0, i32 0), i8 %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @10, i32 0, i32 0))
br label %return

return: ; preds = %.entry
ret void
}

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

define i32 @main() {
.entry:
call void @_lpython_main_program()
ret i32 0
}
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
@@ -400,6 +400,10 @@ filename = "../integration_tests/print_02.py"
asr = true
pass = "print_list"

[[test]]
filename = "../integration_tests/print_04.py"
llvm = true

[[test]]
filename = "../integration_tests/generics_01.py"
asr = true