[llvm-branch-commits] [mlir] [MLIR] Pack some structs using SmallVectorWithFlags (PR #232516)
Mehdi Amini via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Nov 1 03:39:27 PST 2026
https://github.com/joker-eph updated https://github.com/llvm/llvm-project/pull/232516
>From a1c5d33b0b5a8192b956f781fdf1e3db0f27dd5e Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Tue, 6 Oct 2026 08:35:54 -0700
Subject: [PATCH] [MLIR] Pack vector state with SmallVectorWithFlags
Store PresburgerSpace's identifier state, WalkContinuation's action, and
UseListOrderStorage's encoding flag in the capacity fields of their
vectors. Preserve inline capacities and move-based bytecode buffer
transfers; the temporaries in the bytecode reader that only shuffle the
indices stay plain SmallVectors, moved out of the flagged storage.
On x86-64, this reduces PresburgerSpace from 40 to 32 bytes,
WalkContinuation from 72 to 64, and UseListOrderStorage from 40 to 32.
Test flags across empty and heap-backed copies and moves. Cover inline
and heap-backed index pairs and full-shuffle bytecode encoding.
Assisted-by: Codex
Assisted-by: Claude Code
---
.../Analysis/Presburger/PresburgerSpace.h | 27 +++----
mlir/include/mlir/Analysis/SliceWalk.h | 34 +++++---
.../Analysis/Presburger/PresburgerSpace.cpp | 12 +--
mlir/lib/Bytecode/Reader/BytecodeReader.cpp | 23 +++---
mlir/unittests/Analysis/CMakeLists.txt | 5 ++
.../Presburger/PresburgerSpaceTest.cpp | 54 +++++++++++++
mlir/unittests/Analysis/SliceWalkTest.cpp | 81 +++++++++++++++++++
mlir/unittests/Bytecode/BytecodeTest.cpp | 52 ++++++++++++
8 files changed, 246 insertions(+), 42 deletions(-)
create mode 100644 mlir/unittests/Analysis/SliceWalkTest.cpp
diff --git a/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h b/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
index 8e43ec73945826..f029fadbd1b710 100644
--- a/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
+++ b/mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
@@ -15,7 +15,7 @@
#define MLIR_ANALYSIS_PRESBURGER_PRESBURGERSPACE_H
#include "llvm/ADT/ArrayRef.h"
-#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/SmallVectorWithFlags.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
#include "llvm/Support/TypeName.h"
#include "llvm/Support/raw_ostream.h"
@@ -257,19 +257,19 @@ class PresburgerSpace {
/// Get the identifier of pos^th variable of the specified kind.
Identifier getId(VarKind kind, unsigned pos) const {
assert(kind != VarKind::Local && "Local variables have no identifiers");
- if (!usingIds)
+ if (!isUsingIds())
return Identifier();
return identifiers[getVarKindOffset(kind) + pos];
}
ArrayRef<Identifier> getIds(VarKind kind) const {
assert(kind != VarKind::Local && "Local variables have no identifiers");
- assert(usingIds && "Identifiers not enabled for space");
+ assert(isUsingIds() && "Identifiers not enabled for space");
return {identifiers.data() + getVarKindOffset(kind), getNumVarKind(kind)};
}
ArrayRef<Identifier> getIds() const {
- assert(usingIds && "Identifiers not enabled for space");
+ assert(isUsingIds() && "Identifiers not enabled for space");
return identifiers;
}
@@ -277,26 +277,26 @@ class PresburgerSpace {
/// resetIds if identifiers are not enabled.
void setId(VarKind kind, unsigned pos, Identifier id) {
assert(kind != VarKind::Local && "Local variables have no identifiers");
- if (!usingIds)
+ if (!isUsingIds())
resetIds();
identifiers[getVarKindOffset(kind) + pos] = id;
}
/// Returns if identifiers are being used.
- bool isUsingIds() const { return usingIds; }
+ bool isUsingIds() const { return identifiers.getFlag(0); }
- /// Reset the stored identifiers in the space. Enables `usingIds` if it was
- /// `false` before.
+ /// Reset the stored identifiers in the space. Enables identifiers if they
+ /// were disabled before.
void resetIds() {
identifiers.clear();
identifiers.resize(getNumDimAndSymbolVars());
- usingIds = true;
+ identifiers.setFlag(0, true);
}
/// Disable identifiers being stored in space.
void disableIds() {
identifiers.clear();
- usingIds = false;
+ identifiers.setFlag(0, false);
}
/// Check if the spaces are compatible, and the non-local variables having
@@ -338,13 +338,10 @@ class PresburgerSpace {
/// to existentially quantified variables).
unsigned numLocals;
- /// Stores whether or not identifiers are being used in this space.
- bool usingIds = false;
-
/// Stores an identifier for each non-local variable as a `void` pointer.
- SmallVector<Identifier, 0> identifiers;
+ /// Flag bit 0 stores whether identifiers are enabled for this space.
+ llvm::SmallVectorWithFlags<Identifier, 0> identifiers;
};
-
} // namespace presburger
} // namespace mlir
diff --git a/mlir/include/mlir/Analysis/SliceWalk.h b/mlir/include/mlir/Analysis/SliceWalk.h
index eb9ced2ff63b68..ba66226a73b247 100644
--- a/mlir/include/mlir/Analysis/SliceWalk.h
+++ b/mlir/include/mlir/Analysis/SliceWalk.h
@@ -10,9 +10,9 @@
#define MLIR_ANALYSIS_SLICEWALK_H
#include "mlir/IR/ValueRange.h"
+#include "llvm/ADT/SmallVectorWithFlags.h"
namespace mlir {
-
/// A class to signal how to proceed with the walk of the backward slice:
/// - Interrupt: Stops the walk.
/// - AdvanceTo: Continues the walk to user-specified values.
@@ -29,15 +29,16 @@ class WalkContinuation {
};
WalkContinuation(WalkAction action, mlir::ValueRange nextValues)
- : action(action), nextValues(nextValues) {}
+ : nextValues(static_cast<unsigned>(action), nextValues.begin(),
+ nextValues.end()) {}
/// Allows diagnostics to interrupt the walk.
explicit WalkContinuation(mlir::Diagnostic &&)
- : action(WalkAction::Interrupt) {}
+ : WalkContinuation(WalkAction::Interrupt, {}) {}
/// Allows diagnostics to interrupt the walk.
explicit WalkContinuation(mlir::InFlightDiagnostic &&)
- : action(WalkAction::Interrupt) {}
+ : WalkContinuation(WalkAction::Interrupt, {}) {}
/// Creates a continuation that interrupts the walk.
static WalkContinuation interrupt() {
@@ -57,21 +58,33 @@ class WalkContinuation {
}
/// Returns true if the walk was interrupted.
- bool wasInterrupted() const { return action == WalkAction::Interrupt; }
+ bool wasInterrupted() const { return getAction() == WalkAction::Interrupt; }
/// Returns true if the walk was skipped.
- bool wasSkipped() const { return action == WalkAction::Skip; }
+ bool wasSkipped() const { return getAction() == WalkAction::Skip; }
/// Returns true if the walk was advanced to user-specified values.
- bool wasAdvancedTo() const { return action == WalkAction::AdvanceTo; }
+ bool wasAdvancedTo() const { return getAction() == WalkAction::AdvanceTo; }
/// Returns the next values to continue the walk with.
mlir::ArrayRef<mlir::Value> getNextValues() const { return nextValues; }
private:
- WalkAction action;
- /// The next values to continue the walk with.
- mlir::SmallVector<mlir::Value> nextValues;
+ WalkAction getAction() const {
+ return static_cast<WalkAction>(nextValues.getFlags());
+ }
+
+ /// A vector with the same inline capacity as SmallVector<Value>, with two
+ /// flag bits for the walk action.
+ using NextValues = llvm::SmallVectorWithFlags<
+ mlir::Value,
+ llvm::CalculateSmallVectorDefaultInlinedElements<mlir::Value>::value, 2>;
+ static_assert(static_cast<unsigned>(WalkAction::Skip) <
+ (1u << NextValues::NumFlagBits),
+ "WalkAction does not fit in the flag bits");
+
+ /// The next values to continue the walk with; the flags store the action.
+ NextValues nextValues;
};
/// A callback that is invoked for each value encountered during the walk of the
@@ -92,7 +105,6 @@ WalkContinuation walkSlice(mlir::ValueRange rootValues,
/// determine predecessors. Returns nullopt if `value` has no predecessors or
/// when the relevant operations are missing the interface implementations.
std::optional<SmallVector<Value>> getControlFlowPredecessors(Value value);
-
} // namespace mlir
#endif // MLIR_ANALYSIS_SLICEWALK_H
diff --git a/mlir/lib/Analysis/Presburger/PresburgerSpace.cpp b/mlir/lib/Analysis/Presburger/PresburgerSpace.cpp
index 208ee418b26c87..d96d6c77e11bce 100644
--- a/mlir/lib/Analysis/Presburger/PresburgerSpace.cpp
+++ b/mlir/lib/Analysis/Presburger/PresburgerSpace.cpp
@@ -127,9 +127,9 @@ unsigned PresburgerSpace::insertVar(VarKind kind, unsigned pos, unsigned num) {
else
numLocals += num;
- // Insert NULL identifiers if `usingIds` and variables inserted are
+ // Insert NULL identifiers if ids are enabled and variables inserted are
// not locals.
- if (usingIds && kind != VarKind::Local)
+ if (isUsingIds() && kind != VarKind::Local)
identifiers.insert(identifiers.begin() + absolutePos, num, Identifier());
return absolutePos;
@@ -152,9 +152,9 @@ void PresburgerSpace::removeVarRange(VarKind kind, unsigned varStart,
else
numLocals -= numVarsEliminated;
- // Remove identifiers if `usingIds` and variables removed are not
+ // Remove identifiers if ids are enabled and variables removed are not
// locals.
- if (usingIds && kind != VarKind::Local)
+ if (isUsingIds() && kind != VarKind::Local)
identifiers.erase(identifiers.begin() + getVarKindOffset(kind) + varStart,
identifiers.begin() + getVarKindOffset(kind) + varLimit);
}
@@ -168,7 +168,7 @@ void PresburgerSpace::convertVarKind(VarKind srcKind, unsigned srcPos,
assert(dstPos <= getNumVarKind(dstKind) &&
"invalid position for destination variables");
- // Move identifiers if `usingIds` and variables moved are not locals.
+ // Move identifiers if ids are enabled and variables moved are not locals.
unsigned srcOffset = getVarKindOffset(srcKind) + srcPos;
unsigned dstOffset = getVarKindOffset(dstKind) + dstPos;
if (isUsingIds() && srcKind != VarKind::Local && dstKind != VarKind::Local) {
@@ -300,7 +300,7 @@ void PresburgerSpace::setVarSymbolSeparation(unsigned newSymbolCount) {
}
void PresburgerSpace::mergeAndAlignSymbols(PresburgerSpace &other) {
- assert(usingIds && other.usingIds &&
+ assert(isUsingIds() && other.isUsingIds() &&
"Both spaces need to have identifers to merge & align");
// First merge & align identifiers into `other` from `this`.
diff --git a/mlir/lib/Bytecode/Reader/BytecodeReader.cpp b/mlir/lib/Bytecode/Reader/BytecodeReader.cpp
index 96ead28d0aa406..6634d1d784a1e8 100644
--- a/mlir/lib/Bytecode/Reader/BytecodeReader.cpp
+++ b/mlir/lib/Bytecode/Reader/BytecodeReader.cpp
@@ -19,6 +19,7 @@
#include "mlir/Support/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/ScopeExit.h"
+#include "llvm/ADT/SmallVectorWithFlags.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Endian.h"
@@ -1802,17 +1803,17 @@ class mlir::BytecodeReader::Impl {
/// reorder the use-list of a value with respect to the pre-order traversal
/// ordering.
struct UseListOrderStorage {
- UseListOrderStorage(bool isIndexPairEncoding,
- SmallVector<unsigned, 4> &&indices)
- : indices(std::move(indices)),
- isIndexPairEncoding(isIndexPairEncoding) {};
+ using IndexStorage = llvm::SmallVectorWithFlags<unsigned, 4>;
+
+ UseListOrderStorage(bool isIndexPairEncoding, IndexStorage &&indices)
+ : indices(isIndexPairEncoding, std::move(indices)) {}
/// The vector containing the information required to reorder the
/// use-list of a value.
- SmallVector<unsigned, 4> indices;
+ IndexStorage indices;
/// Whether indices represent a pair of type `(src, dst)` or it is a direct
/// indexing, such as `dst = order[src]`.
- bool isIndexPairEncoding;
+ bool isIndexPairEncoding() const { return indices.getFlag(0); }
};
/// Parse use-list order from bytecode for a range of values if available. The
@@ -2235,7 +2236,7 @@ BytecodeReader::Impl::parseUseListOrderForRange(EncodingReader &reader,
if (failed(reader.parseVarIntWithFlag(numValues, indexPairEncoding)))
return failure();
- SmallVector<unsigned, 4> useListOrders;
+ UseListOrderStorage::IndexStorage useListOrders;
for (size_t idx = 0; idx < numValues; idx++) {
uint64_t index;
if (failed(reader.parseVarInt(index)))
@@ -2303,14 +2304,16 @@ LogicalResult BytecodeReader::Impl::sortUseListOrder(Value value) {
// Pull the custom order info from the map.
UseListOrderStorage customOrder =
valueToUseListMap.at(value.getAsOpaquePointer());
- SmallVector<unsigned, 4> shuffle = std::move(customOrder.indices);
+ // Read the encoding flag before moving the indices out of the storage.
+ bool isIndexPairEncoding = customOrder.isIndexPairEncoding();
+ SmallVector<unsigned, 4> shuffle(std::move(customOrder.indices));
uint64_t numUses = value.getNumUses();
// If the encoding was a pair of indices `(src, dst)` for every permutation,
// reconstruct the shuffle vector for every use. Initialize the shuffle vector
// as identity, and then apply the mapping encoded in the indices.
// This produces shuffle[oldIdx] = newPos (i.e., old_index -> new_position).
- if (customOrder.isIndexPairEncoding) {
+ if (isIndexPairEncoding) {
// Return failure if the number of indices was not representing pairs.
if (shuffle.size() & 1)
return failure();
@@ -2355,7 +2358,7 @@ LogicalResult BytecodeReader::Impl::sortUseListOrder(Value value) {
// a given sorted position. We fold the permutation inversion for the
// full-shuffle case directly into the composition to avoid an extra pass.
SmallVector<unsigned, 4> finalShuffle(numUses);
- if (customOrder.isIndexPairEncoding) {
+ if (isIndexPairEncoding) {
for (size_t writerMemIdx = 0; writerMemIdx < numUses; ++writerMemIdx)
finalShuffle[currentOrder[shuffle[writerMemIdx]].first] = writerMemIdx;
} else {
diff --git a/mlir/unittests/Analysis/CMakeLists.txt b/mlir/unittests/Analysis/CMakeLists.txt
index a05d09f89070cf..b493ca51559a7f 100644
--- a/mlir/unittests/Analysis/CMakeLists.txt
+++ b/mlir/unittests/Analysis/CMakeLists.txt
@@ -1 +1,6 @@
add_subdirectory(Presburger)
+
+add_mlir_unittest(MLIRAnalysisTests
+ SliceWalkTest.cpp
+)
+mlir_target_link_libraries(MLIRAnalysisTests PRIVATE MLIRAnalysis MLIRIR)
diff --git a/mlir/unittests/Analysis/Presburger/PresburgerSpaceTest.cpp b/mlir/unittests/Analysis/Presburger/PresburgerSpaceTest.cpp
index 7229a8ab6b4c47..9b3f9da0394e3a 100644
--- a/mlir/unittests/Analysis/Presburger/PresburgerSpaceTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/PresburgerSpaceTest.cpp
@@ -255,3 +255,57 @@ TEST(PresburgerSpaceTest, mergeAndAlignSymbols) {
EXPECT_EQ(otherSpace.getId(VarKind::Range, 1),
Identifier(&otherIdentifiers[4]));
}
+
+TEST(PresburgerSpaceTest, copyAndMoveIdentifierState) {
+ // Enabled empty storage must stay distinct from disabled empty storage.
+ // Nonempty storage also exercises moving a heap allocation.
+ for (unsigned numDims : {0u, 16u}) {
+ auto space = PresburgerSpace::getSetSpace(numDims);
+ EXPECT_FALSE(space.isUsingIds());
+ space.resetIds();
+ EXPECT_TRUE(space.isUsingIds());
+ EXPECT_EQ(space.getIds().size(), numDims);
+
+ // Growing enabled but empty inline storage onto the heap keeps the flag.
+ if (numDims == 0) {
+ space.insertVar(VarKind::SetDim, 0, 32);
+ EXPECT_TRUE(space.isUsingIds());
+ EXPECT_EQ(space.getIds().size(), 32u);
+ space.removeVarRange(VarKind::SetDim, 0, 32);
+ EXPECT_TRUE(space.isUsingIds());
+ EXPECT_EQ(space.getIds().size(), 0u);
+ }
+
+ int id = 0;
+ if (numDims)
+ space.setId(VarKind::SetDim, 0, Identifier(&id));
+ auto copy = space;
+ auto moved = std::move(copy);
+ EXPECT_TRUE(moved.isUsingIds());
+ EXPECT_EQ(moved.getIds().size(), numDims);
+ if (numDims)
+ EXPECT_EQ(moved.getId(VarKind::SetDim, 0), Identifier(&id));
+
+ auto assigned = PresburgerSpace::getSetSpace();
+ assigned = space;
+ EXPECT_TRUE(assigned.isUsingIds());
+ EXPECT_EQ(assigned.getIds().size(), numDims);
+ assigned = std::move(moved);
+ EXPECT_TRUE(assigned.isUsingIds());
+ EXPECT_EQ(assigned.getIds().size(), numDims);
+ if (numDims)
+ EXPECT_EQ(assigned.getId(VarKind::SetDim, 0), Identifier(&id));
+
+ assigned.disableIds();
+ EXPECT_FALSE(assigned.isUsingIds());
+ EXPECT_TRUE(space.isUsingIds());
+ assigned.insertVar(VarKind::SetDim, numDims, 32);
+ EXPECT_FALSE(assigned.isUsingIds());
+ assigned.resetIds();
+ EXPECT_TRUE(assigned.isUsingIds());
+ EXPECT_EQ(assigned.getIds().size(), numDims + 32);
+
+ assigned = PresburgerSpace::getSetSpace();
+ EXPECT_FALSE(assigned.isUsingIds());
+ }
+}
diff --git a/mlir/unittests/Analysis/SliceWalkTest.cpp b/mlir/unittests/Analysis/SliceWalkTest.cpp
new file mode 100644
index 00000000000000..5a9db6c4d0b75c
--- /dev/null
+++ b/mlir/unittests/Analysis/SliceWalkTest.cpp
@@ -0,0 +1,81 @@
+//===- SliceWalkTest.cpp - WalkContinuation tests -------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Analysis/SliceWalk.h"
+#include "mlir/IR/Block.h"
+#include "mlir/IR/BuiltinTypes.h"
+#include "mlir/IR/Diagnostics.h"
+#include "mlir/IR/MLIRContext.h"
+#include "gtest/gtest.h"
+
+using namespace mlir;
+
+// The action is stored in the flag bits of the empty value vector, so it must
+// survive copies and moves that transfer no elements.
+TEST(WalkContinuationTest, CopyAndMoveEmptyContinuations) {
+ EXPECT_TRUE(WalkContinuation::interrupt().wasInterrupted());
+ EXPECT_TRUE(WalkContinuation::skip().wasSkipped());
+ EXPECT_TRUE(WalkContinuation::advanceTo({}).wasAdvancedTo());
+ for (auto continuation :
+ {WalkContinuation::interrupt(), WalkContinuation::skip(),
+ WalkContinuation::advanceTo({})}) {
+ // Exactly one action is reported.
+ EXPECT_EQ(1, continuation.wasInterrupted() + continuation.wasSkipped() +
+ continuation.wasAdvancedTo());
+ auto copy = continuation;
+ auto moved = std::move(copy);
+ EXPECT_EQ(moved.wasInterrupted(), continuation.wasInterrupted());
+ EXPECT_EQ(moved.wasSkipped(), continuation.wasSkipped());
+ EXPECT_EQ(moved.wasAdvancedTo(), continuation.wasAdvancedTo());
+ EXPECT_TRUE(moved.getNextValues().empty());
+
+ auto assigned = WalkContinuation::interrupt();
+ assigned = continuation;
+ EXPECT_EQ(assigned.wasSkipped(), continuation.wasSkipped());
+ EXPECT_EQ(assigned.wasAdvancedTo(), continuation.wasAdvancedTo());
+ assigned = std::move(moved);
+ EXPECT_EQ(assigned.wasInterrupted(), continuation.wasInterrupted());
+ EXPECT_EQ(assigned.wasSkipped(), continuation.wasSkipped());
+ EXPECT_EQ(assigned.wasAdvancedTo(), continuation.wasAdvancedTo());
+ }
+}
+
+TEST(WalkContinuationTest, CopyAndMoveHeapContinuations) {
+ MLIRContext context;
+ Block block;
+ for (unsigned i = 0; i < 32; ++i)
+ block.addArgument(IntegerType::get(&context, 32),
+ UnknownLoc::get(&context));
+ SmallVector<Value> values(block.getArguments());
+
+ auto continuation = WalkContinuation::advanceTo(values);
+ auto copy = continuation;
+ auto moved = std::move(copy);
+ EXPECT_TRUE(moved.wasAdvancedTo());
+ EXPECT_EQ(moved.getNextValues(), ArrayRef<Value>(values));
+
+ auto assigned = WalkContinuation::skip();
+ assigned = continuation;
+ EXPECT_TRUE(assigned.wasAdvancedTo());
+ EXPECT_EQ(assigned.getNextValues(), ArrayRef<Value>(values));
+ assigned = std::move(moved);
+ EXPECT_TRUE(assigned.wasAdvancedTo());
+ EXPECT_EQ(assigned.getNextValues(), ArrayRef<Value>(values));
+
+ assigned = WalkContinuation::skip();
+ EXPECT_TRUE(assigned.wasSkipped());
+ EXPECT_TRUE(assigned.getNextValues().empty());
+}
+
+TEST(WalkContinuationTest, DiagnosticInterrupts) {
+ MLIRContext context;
+ WalkContinuation continuation(
+ Diagnostic(UnknownLoc::get(&context), DiagnosticSeverity::Error));
+ EXPECT_TRUE(continuation.wasInterrupted());
+ EXPECT_TRUE(continuation.getNextValues().empty());
+}
diff --git a/mlir/unittests/Bytecode/BytecodeTest.cpp b/mlir/unittests/Bytecode/BytecodeTest.cpp
index b6eb84fb4e60b3..f2c91fd6dd3598 100644
--- a/mlir/unittests/Bytecode/BytecodeTest.cpp
+++ b/mlir/unittests/Bytecode/BytecodeTest.cpp
@@ -9,6 +9,7 @@
#include "mlir/Bytecode/BytecodeReader.h"
#include "mlir/Bytecode/BytecodeWriter.h"
#include "mlir/IR/AsmState.h"
+#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/OpImplementation.h"
@@ -24,6 +25,8 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
+#include <numeric>
+
using namespace llvm;
using namespace mlir;
@@ -405,3 +408,52 @@ TEST(Bytecode, LocationElisionPreservesAttributes) {
ASSERT_TRUE(nameLoc);
EXPECT_EQ(nameLoc.getName().getValue(), "preserve_me");
}
+
+TEST(Bytecode, UseListEncodingFlags) {
+ // The writer uses index pairs while fewer than half of the 16 uses move:
+ // one swapped pair uses inline index-pair storage, three use heap index-pair
+ // storage, and four (the boundary) and eight use the full-shuffle encoding.
+ for (unsigned numPairs : {1u, 3u, 4u, 8u}) {
+ SCOPED_TRACE(numPairs);
+ MLIRContext context;
+ context.allowUnregisteredDialects();
+ Builder builder(&context);
+ Location loc = builder.getUnknownLoc();
+ OwningOpRef<ModuleOp> module = ModuleOp::create(loc);
+
+ OperationState producerState(loc, "test.producer");
+ producerState.addTypes(builder.getI32Type());
+ Operation *producer = Operation::create(producerState);
+ module->getBody()->push_back(producer);
+ Value value = producer->getResult(0);
+ for (unsigned i = 0; i < 16; ++i) {
+ OperationState userState(loc, "test.use");
+ userState.addOperands(value);
+ userState.addAttribute("index", builder.getI32IntegerAttr(i));
+ module->getBody()->push_back(Operation::create(userState));
+ }
+
+ SmallVector<unsigned> permutation(16);
+ std::iota(permutation.begin(), permutation.end(), 0);
+ for (unsigned i = 0; i < numPairs; ++i)
+ std::swap(permutation[2 * i], permutation[2 * i + 1]);
+ value.shuffleUseList(permutation);
+
+ auto getUseOrder = [](Value value) {
+ SmallVector<int64_t> order;
+ for (OpOperand &use : value.getUses())
+ order.push_back(
+ cast<IntegerAttr>(use.getOwner()->getAttr("index")).getInt());
+ return order;
+ };
+ auto expectedOrder = getUseOrder(value);
+
+ std::string bytecode;
+ raw_string_ostream os(bytecode);
+ ASSERT_TRUE(succeeded(writeBytecodeToFile(module.get(), os)));
+ auto roundtripped = parseSourceString<ModuleOp>(bytecode, &context);
+ ASSERT_TRUE(roundtripped);
+ Value parsedValue = roundtripped->getBody()->front().getResult(0);
+ EXPECT_EQ(getUseOrder(parsedValue), expectedOrder);
+ }
+}
More information about the llvm-branch-commits
mailing list