Skip to content

Commit

Permalink
[CIR] Incorrect global view index and offset when neg index (llvm#795)
Browse files Browse the repository at this point in the history
mixed signed and unsigned integer operator cause difference result when
index of array is negative
  • Loading branch information
HerrCai0907 authored and lanza committed Oct 2, 2024
1 parent 66ba348 commit 5758cef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
25 changes: 16 additions & 9 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <cassert>
#include <optional>
#include <string>
#include <utility>

namespace cir {

Expand Down Expand Up @@ -869,16 +870,21 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {

mlir::Type SubType;

auto getIndexAndNewOffset =
[](int64_t Offset, int64_t EltSize) -> std::pair<int64_t, int64_t> {
int64_t DivRet = Offset / EltSize;
if (DivRet < 0)
DivRet -= 1; // make sure offset is positive
int64_t ModRet = Offset - (DivRet * EltSize);
return {DivRet, ModRet};
};

if (auto ArrayTy = mlir::dyn_cast<mlir::cir::ArrayType>(Ty)) {
auto EltSize = Layout.getTypeAllocSize(ArrayTy.getEltType());
Indices.push_back(Offset / EltSize);
int64_t EltSize = Layout.getTypeAllocSize(ArrayTy.getEltType());
SubType = ArrayTy.getEltType();
Offset %= EltSize;
} else if (auto PtrTy = mlir::dyn_cast<mlir::cir::PointerType>(Ty)) {
auto EltSize = Layout.getTypeAllocSize(PtrTy.getPointee());
Indices.push_back(Offset / EltSize);
SubType = PtrTy.getPointee();
Offset %= EltSize;
auto const [Index, NewOffset] = getIndexAndNewOffset(Offset, EltSize);
Indices.push_back(Index);
Offset = NewOffset;
} else if (auto StructTy = mlir::dyn_cast<mlir::cir::StructType>(Ty)) {
auto Elts = StructTy.getMembers();
int64_t Pos = 0;
Expand All @@ -887,7 +893,8 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
(int64_t)Layout.getTypeAllocSize(Elts[I]).getFixedValue();
unsigned AlignMask = Layout.getABITypeAlign(Elts[I]).value() - 1;
Pos = (Pos + AlignMask) & ~AlignMask;
if (Offset < Pos + EltSize) {
assert(Offset >= 0);
if (static_cast<uint64_t>(Offset) < Pos + EltSize) {
Indices.push_back(I);
SubType = Elts[I];
Offset -= Pos;
Expand Down
21 changes: 21 additions & 0 deletions clang/test/CIR/CodeGen/globals-neg-index-array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-linux-gnu -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s
// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
// XFAIL: *

struct __attribute__((packed)) PackedStruct {
char a1;
char a2;
char a3;
};
struct PackedStruct packed[10];
char *packed_element = &(packed[-2].a3);
// CHECK: cir.global external @packed = #cir.zero : !cir.array<!ty_22PackedStruct22 x 10> loc(#loc5)
// CHECK: cir.global external @packed_element = #cir.global_view<@packed, [-2 : i32, 2 : i32]>
// LLVM: @packed = global [10 x %struct.PackedStruct] zeroinitializer
// LLVM: @packed_element = global ptr getelementptr inbounds ([10 x %struct.PackedStruct], ptr @packed, i32 -2, i32 2)

0 comments on commit 5758cef

Please sign in to comment.