[AffineMap] Move result exprs into trailing storage. NFCI.
authorBenjamin Kramer <benny.kra@googlemail.com>
Sat, 12 Feb 2022 14:23:41 +0000 (15:23 +0100)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sat, 12 Feb 2022 14:24:00 +0000 (15:24 +0100)
mlir/lib/IR/AffineMap.cpp
mlir/lib/IR/AffineMapDetail.h

index 73cbd3b..5d0ffd6 100644 (file)
@@ -299,22 +299,17 @@ unsigned AffineMap::getNumSymbols() const {
   assert(map && "uninitialized map storage");
   return map->numSymbols;
 }
-unsigned AffineMap::getNumResults() const {
-  assert(map && "uninitialized map storage");
-  return map->results.size();
-}
+unsigned AffineMap::getNumResults() const { return getResults().size(); }
 unsigned AffineMap::getNumInputs() const {
   assert(map && "uninitialized map storage");
   return map->numDims + map->numSymbols;
 }
-
 ArrayRef<AffineExpr> AffineMap::getResults() const {
   assert(map && "uninitialized map storage");
-  return map->results;
+  return map->results();
 }
 AffineExpr AffineMap::getResult(unsigned idx) const {
-  assert(map && "uninitialized map storage");
-  return map->results[idx];
+  return getResults()[idx];
 }
 
 unsigned AffineMap::getDimPosition(unsigned idx) const {
index c0a906b..732c7fd 100644 (file)
 #include "mlir/IR/AffineMap.h"
 #include "mlir/Support/StorageUniquer.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/Support/TrailingObjects.h"
 
 namespace mlir {
 namespace detail {
 
-struct AffineMapStorage : public StorageUniquer::BaseStorage {
+struct AffineMapStorage final
+    : public StorageUniquer::BaseStorage,
+      public llvm::TrailingObjects<AffineMapStorage, AffineExpr> {
   /// The hash key used for uniquing.
   using KeyTy = std::tuple<unsigned, unsigned, ArrayRef<AffineExpr>>;
 
   unsigned numDims;
   unsigned numSymbols;
-
-  /// The affine expressions for this (multi-dimensional) map.
-  /// TODO: use trailing objects for this.
-  ArrayRef<AffineExpr> results;
+  unsigned numResults;
 
   MLIRContext *context;
 
+  /// The affine expressions for this (multi-dimensional) map.
+  ArrayRef<AffineExpr> results() const {
+    return {getTrailingObjects<AffineExpr>(), numResults};
+  }
+
   bool operator==(const KeyTy &key) const {
     return std::get<0>(key) == numDims && std::get<1>(key) == numSymbols &&
-           std::get<2>(key) == results;
+           std::get<2>(key) == results();
   }
 
   // Constructs an AffineMapStorage from a key. The context must be set by the
   // caller.
   static AffineMapStorage *
   construct(StorageUniquer::StorageAllocator &allocator, const KeyTy &key) {
-    auto *res = new (allocator.allocate<AffineMapStorage>()) AffineMapStorage();
+    auto results = std::get<2>(key);
+    auto byteSize =
+        AffineMapStorage::totalSizeToAlloc<AffineExpr>(results.size());
+    auto *rawMem = allocator.allocate(byteSize, alignof(AffineMapStorage));
+    auto *res = new (rawMem) AffineMapStorage();
     res->numDims = std::get<0>(key);
     res->numSymbols = std::get<1>(key);
-    res->results = allocator.copyInto(std::get<2>(key));
+    res->numResults = results.size();
+    std::uninitialized_copy(results.begin(), results.end(),
+                            res->getTrailingObjects<AffineExpr>());
     return res;
   }
 };