Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions ir/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,18 @@ unsigned AggregateType::countPaddings(unsigned to_idx) const {
return count;
}

unsigned AggregateType::paddedIndex(unsigned idx) const {
unsigned seen = 0;
for (unsigned i = 0; i < elements; ++i) {
if (is_padding[i])
continue;
if (seen == idx)
return i;
++seen;
}
UNREACHABLE();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is UB for an out-of-bounds idx. Also I think the logic could be simplified. Maybe something like

unsigned AggregateType::paddedIndex(unsigned idx) const {
  assert(idx < elements - numPaddingsConst());
  for (unsigned i = 0; ; ++i) {
    if (!is_padding[i] && idx-- == 0)
      return i;
  }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in alive2 UNREACHABLE() becomes __builtin_unreachable() which turns into a trap, so the effect would be the same, assuming your assert and my UNREACHABLE() are equisatisfiable

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From clang docs:

The __builtin_unreachable() builtin has completely undefined behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes of course. but it turns into a trap in practice.

}

expr AggregateType::numElementsExcludingPadding() const {
auto elems = numElements();
return numElements() - expr::mkInt(numPaddingsConst(), elems);
Expand Down
2 changes: 2 additions & 0 deletions ir/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ class AggregateType : public Type {
Type& getChild(unsigned index) const { return *children[index]; }
bool isPadding(unsigned i) const { return is_padding[i]; }
unsigned countPaddings(unsigned to_idx) const;
// Maps an index that ignores paddings to the index in children.
unsigned paddedIndex(unsigned idx) const;

unsigned bits() const override;
unsigned np_bits(bool fromInt) const override;
Expand Down
4 changes: 2 additions & 2 deletions llvm_util/llvm2alive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
auto *valty = &val->getType();
for (auto idx : i.indices()) {
auto *aty = valty->getAsAggregateType();
unsigned idx_with_paddings = aty->countPaddings(idx) + idx;
unsigned idx_with_paddings = aty->paddedIndex(idx);
inst->addIdx(idx_with_paddings);
valty = &aty->getChild(idx_with_paddings);
}
Expand All @@ -585,7 +585,7 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {

for (auto idx : i.indices()) {
auto *aty = ty->getAsAggregateType();
unsigned idx_with_paddings = aty->countPaddings(idx) + idx;
unsigned idx_with_paddings = aty->paddedIndex(idx);
inst->addIdx(idx_with_paddings);
ty = &aty->getChild(idx_with_paddings);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/alive-tv/extractvalue-padding2.srctgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define i32 @src({ i8, i16, i8, i32 } %x) {
%a = insertvalue { i8, i16, i8, i32 } %x, i32 7, 3
%b = extractvalue { i8, i16, i8, i32 } %a, 3
ret i32 %b
}

define i32 @tgt({ i8, i16, i8, i32 } %x) {
ret i32 7
}
8 changes: 8 additions & 0 deletions tests/alive-tv/extractvalue-padding3.srctgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
define i24 @src() {
%a = extractvalue [5 x i24] [i24 0, i24 1, i24 2, i24 3, i24 4], 4
ret i24 %a
}

define i24 @tgt() {
ret i24 4
}
4 changes: 4 additions & 0 deletions tests/alive-tv/insertvalue-padding2.ident.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
define { i8, i16, i8, i32 } @f() {
%a = insertvalue { i8, i16, i8, i32 } poison, i32 1, 3
ret { i8, i16, i8, i32 } %a
}
Loading