-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fee246
commit 4c565fe
Showing
6 changed files
with
95 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
|
||
void print(int32_t x) { | ||
printf("%d\n", x); | ||
} | ||
|
||
int32_t read() { | ||
int32_t x; | ||
scanf("%d", &x); | ||
return x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// RUN: %gccjit-opt %s \ | ||
// RUN: -lower-affine \ | ||
// RUN: -convert-scf-to-cf \ | ||
// RUN: -convert-arith-to-gccjit \ | ||
// RUN: -convert-memref-to-gccjit \ | ||
// RUN: -convert-func-to-gccjit \ | ||
// RUN: -reconcile-unrealized-casts -mlir-print-debuginfo -o %t.mlir | ||
|
||
// RUN: %gccjit-translate %t.mlir -mlir-to-gccjit-gimple | %filecheck %s --check-prefix=CHECK-GIMPLE | ||
// RUN: %gccjit-translate %t.mlir -mlir-to-gccjit-dylib -o %t.so | ||
// RUN: cc -O3 %p/alloca_sum.c %t.so -Wl,-rpath,%T -o %t.exe | ||
// RUN: seq 1 100 | %t.exe | %filecheck %s --check-prefix=CHECK-OUTPUT | ||
|
||
// CHECK-OUTPUT: 5050 | ||
module attributes { gccjit.opt_level = #gccjit.opt_level<O3>, gccjit.debug_info = false } { | ||
// Import C standard library functions for I/O | ||
func.func private @read() -> i32 | ||
func.func private @print(%val: i32) | ||
|
||
func.func @main() -> i32 { | ||
// Allocate memory for the 100 integers array and initialize sum to 0 | ||
// CHECK-GIMPLE: %{{[0-9]+}} = bitcast(alloca (%{{[0-9]+}}), __uint32_t *); | ||
%array = memref.alloca() : memref<100xi32> | ||
%sum_init = arith.constant 0 : i32 | ||
|
||
// Loop to read 100 integers into the array | ||
%c0 = arith.constant 0 : index | ||
%c1 = arith.constant 1 : index | ||
%c100 = arith.constant 100 : index | ||
|
||
scf.for %i = %c0 to %c100 step %c1 { | ||
// Call scanf to read an integer | ||
// CHECK-GIMPLE: %{{[0-9]+}} = read (); | ||
%read = func.call @read() : () -> i32 | ||
memref.store %read, %array[%i] : memref<100xi32> | ||
} | ||
|
||
// Loop to calculate the sum using iter_args | ||
%final_sum = scf.for %i = %c0 to %c100 step %c1 iter_args(%acc = %sum_init) -> (i32) { | ||
%elem = memref.load %array[%i] : memref<100xi32> | ||
%new_sum = arith.addi %acc, %elem : i32 | ||
scf.yield %new_sum : i32 | ||
} | ||
|
||
// Print the result using printf | ||
// CHECK-GIMPLE: (void)print (%{{[0-9]+}}); | ||
func.call @print(%final_sum) : (i32) -> () | ||
|
||
%c0_1 = arith.constant 0 : i32 | ||
return %c0_1 : i32 | ||
} | ||
} |