forked from llvm/clangir
-
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.
[CIR][CodeGen][Lowering] Support Integer overflow with fwrap (llvm#539)
This PR fixes some cases when a program compiled with `-fwrapv` fails with `NYI` . Basically, the default behavior is no overlap: ``` void baz(int x, int y) { int z = x - y; } ``` LLVM IR (no CIR enabled): ``` %sub = sub nsw i32 %0, %1 ``` and with `-fwrapv` : ``` %sub = sub i32 %0, %1 ``` We need something similar in CIR. The only way I see how to implement it is to add a couple of attributes to the `BinOp` to make things even with the llvm dialect. Well, are there any other ideas? --------- Co-authored-by: Bruno Cardoso Lopes <[email protected]>
- Loading branch information
Showing
10 changed files
with
141 additions
and
20 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
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,30 @@ | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fwrapv -fclangir -emit-cir %s -o - 2>&1 | FileCheck %s --check-prefix=WRAP | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - 2>&1 | FileCheck %s --check-prefix=NOWRAP | ||
|
||
#define N 42 | ||
|
||
typedef struct { | ||
const char* ptr; | ||
} A; | ||
|
||
// WRAP: cir.binop(sub, {{.*}}, {{.*}}) : !s32i | ||
// NOWRAP: cir.binop(sub, {{.*}}, {{.*}}) nsw : !s32i | ||
void foo(int* ar, int len) { | ||
int x = ar[len - N]; | ||
} | ||
|
||
// check that the ptr_stride is generated in both cases (i.e. no NYI fails) | ||
|
||
// WRAP: cir.ptr_stride | ||
// NOWRAP: cir.ptr_stride | ||
void bar(A* a, unsigned n) { | ||
a->ptr = a->ptr + n; | ||
} | ||
|
||
// WRAP cir.ptr_stride | ||
// NOWRAP: cir.ptr_stride | ||
void baz(A* a) { | ||
a->ptr--; | ||
} | ||
|
||
|
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,24 @@ | ||
// RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR | ||
// RUN: cir-translate %s -cir-to-llvmir | FileCheck %s -check-prefix=LLVM | ||
|
||
!s32i = !cir.int<s, 32> | ||
module { | ||
cir.func @test(%arg0: !s32i) { | ||
%0 = cir.alloca !s32i, cir.ptr <!s32i>, ["len", init] {alignment = 4 : i64} | ||
cir.store %arg0, %0 : !s32i, cir.ptr <!s32i> | ||
%1 = cir.load %0 : cir.ptr <!s32i>, !s32i | ||
%2 = cir.const(#cir.int<42> : !s32i) : !s32i | ||
%3 = cir.binop(sub, %1, %2) nsw : !s32i | ||
%4 = cir.binop(sub, %1, %2) nuw : !s32i | ||
%5 = cir.binop(sub, %1, %2) : !s32i | ||
cir.return | ||
} | ||
} | ||
|
||
// MLIR: llvm.sub {{.*}}, {{.*}} overflow<nsw> : i32 | ||
// MLIR-NEXT: llvm.sub {{.*}}, {{.*}} overflow<nuw> : i32 | ||
// MLIR-NEXT: llvm.sub {{.*}}, {{.*}} : i32 | ||
|
||
// LLVM: sub nsw i32 {{.*}}, {{.*}}, !dbg !9 | ||
// LLVM-NEXT: sub nuw i32 {{.*}}, {{.*}}, !dbg !10 | ||
// LLVM-NEXT: sub i32 {{.*}}, {{.*}}, !dbg !11 |