[Mlir-commits] [mlir] 772994f - [mlir:Bytecode] Add encoding support for builtin location attributes

River Riddle llvmlistbot at llvm.org
Fri Aug 26 13:33:44 PDT 2022


Author: River Riddle
Date: 2022-08-26T13:31:06-07:00
New Revision: 772994f1e088f71927844a2ca0f7bc60e33d7900

URL: https://github.com/llvm/llvm-project/commit/772994f1e088f71927844a2ca0f7bc60e33d7900
DIFF: https://github.com/llvm/llvm-project/commit/772994f1e088f71927844a2ca0f7bc60e33d7900.diff

LOG: [mlir:Bytecode] Add encoding support for builtin location attributes

This provides a significantly more efficient encoding for locations.

Differential Revision: https://reviews.llvm.org/D132540

Added: 
    

Modified: 
    mlir/lib/IR/BuiltinDialectBytecode.cpp
    mlir/test/Dialect/Builtin/Bytecode/attrs.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/IR/BuiltinDialectBytecode.cpp b/mlir/lib/IR/BuiltinDialectBytecode.cpp
index 4c47aceff12b9..555626ba7336a 100644
--- a/mlir/lib/IR/BuiltinDialectBytecode.cpp
+++ b/mlir/lib/IR/BuiltinDialectBytecode.cpp
@@ -81,6 +81,41 @@ enum AttributeCode {
   ///     value: APFloat
   ///   }
   kFloatAttr = 9,
+
+  ///   CallSiteLoc {
+  ///    callee: LocationAttr,
+  ///    caller: LocationAttr
+  ///   }
+  kCallSiteLoc = 10,
+
+  ///   FileLineColLoc {
+  ///     file: StringAttr,
+  ///     line: varint,
+  ///     column: varint
+  ///   }
+  kFileLineColLoc = 11,
+
+  ///   FusedLoc {
+  ///     locations: LocationAttr[]
+  ///   }
+  kFusedLoc = 12,
+
+  ///   FusedLocWithMetadata {
+  ///     locations: LocationAttr[],
+  ///     metadata: Attribute
+  ///   }
+  /// A variant of FusedLoc with metadata.
+  kFusedLocWithMetadata = 13,
+
+  ///   NameLoc {
+  ///     name: StringAttr,
+  ///     childLoc: LocationAttr
+  ///   }
+  kNameLoc = 14,
+
+  ///   UnknownLoc {
+  ///   }
+  kUnknownLoc = 15,
 };
 
 /// This enum contains marker codes used to indicate which type is currently
@@ -133,6 +168,12 @@ struct BuiltinDialectBytecodeInterface : public BytecodeDialectInterface {
                                   bool hasNestedRefs) const;
   TypeAttr readTypeAttr(DialectBytecodeReader &reader) const;
 
+  LocationAttr readCallSiteLoc(DialectBytecodeReader &reader) const;
+  LocationAttr readFileLineColLoc(DialectBytecodeReader &reader) const;
+  LocationAttr readFusedLoc(DialectBytecodeReader &reader,
+                            bool hasMetadata) const;
+  LocationAttr readNameLoc(DialectBytecodeReader &reader) const;
+
   LogicalResult writeAttribute(Attribute attr,
                                DialectBytecodeWriter &writer) const override;
   void write(ArrayAttr attr, DialectBytecodeWriter &writer) const;
@@ -143,6 +184,12 @@ struct BuiltinDialectBytecodeInterface : public BytecodeDialectInterface {
   void write(SymbolRefAttr attr, DialectBytecodeWriter &writer) const;
   void write(TypeAttr attr, DialectBytecodeWriter &writer) const;
 
+  void write(CallSiteLoc attr, DialectBytecodeWriter &writer) const;
+  void write(FileLineColLoc attr, DialectBytecodeWriter &writer) const;
+  void write(FusedLoc attr, DialectBytecodeWriter &writer) const;
+  void write(NameLoc attr, DialectBytecodeWriter &writer) const;
+  LogicalResult write(OpaqueLoc attr, DialectBytecodeWriter &writer) const;
+
   //===--------------------------------------------------------------------===//
   // Types
 
@@ -190,6 +237,18 @@ Attribute BuiltinDialectBytecodeInterface::readAttribute(
     return readIntegerAttr(reader);
   case builtin_encoding::kFloatAttr:
     return readFloatAttr(reader);
+  case builtin_encoding::kCallSiteLoc:
+    return readCallSiteLoc(reader);
+  case builtin_encoding::kFileLineColLoc:
+    return readFileLineColLoc(reader);
+  case builtin_encoding::kFusedLoc:
+    return readFusedLoc(reader, /*hasMetadata=*/false);
+  case builtin_encoding::kFusedLocWithMetadata:
+    return readFusedLoc(reader, /*hasMetadata=*/true);
+  case builtin_encoding::kNameLoc:
+    return readNameLoc(reader);
+  case builtin_encoding::kUnknownLoc:
+    return UnknownLoc::get(getContext());
   default:
     reader.emitError() << "unknown builtin attribute code: " << code;
     return Attribute();
@@ -291,6 +350,57 @@ TypeAttr BuiltinDialectBytecodeInterface::readTypeAttr(
   return TypeAttr::get(type);
 }
 
+LocationAttr BuiltinDialectBytecodeInterface::readCallSiteLoc(
+    DialectBytecodeReader &reader) const {
+  LocationAttr callee, caller;
+  if (failed(reader.readAttribute(callee)) ||
+      failed(reader.readAttribute(caller)))
+    return LocationAttr();
+  return CallSiteLoc::get(callee, caller);
+}
+
+LocationAttr BuiltinDialectBytecodeInterface::readFileLineColLoc(
+    DialectBytecodeReader &reader) const {
+  StringAttr filename;
+  uint64_t line, column;
+  if (failed(reader.readAttribute(filename)) ||
+      failed(reader.readVarInt(line)) || failed(reader.readVarInt(column)))
+    return LocationAttr();
+  return FileLineColLoc::get(filename, line, column);
+}
+
+LocationAttr
+BuiltinDialectBytecodeInterface::readFusedLoc(DialectBytecodeReader &reader,
+                                              bool hasMetadata) const {
+  // Parse the child locations.
+  auto readLoc = [&]() -> FailureOr<Location> {
+    LocationAttr locAttr;
+    if (failed(reader.readAttribute(locAttr)))
+      return failure();
+    return Location(locAttr);
+  };
+  SmallVector<Location> locations;
+  if (failed(reader.readList(locations, readLoc)))
+    return LocationAttr();
+
+  // Parse the metadata if present.
+  Attribute metadata;
+  if (hasMetadata && failed(reader.readAttribute(metadata)))
+    return LocationAttr();
+
+  return FusedLoc::get(locations, metadata, getContext());
+}
+
+LocationAttr BuiltinDialectBytecodeInterface::readNameLoc(
+    DialectBytecodeReader &reader) const {
+  StringAttr name;
+  LocationAttr childLoc;
+  if (failed(reader.readAttribute(name)) ||
+      failed(reader.readAttribute(childLoc)))
+    return LocationAttr();
+  return NameLoc::get(name, childLoc);
+}
+
 //===----------------------------------------------------------------------===//
 // Attributes: Writer
 
@@ -298,14 +408,20 @@ LogicalResult BuiltinDialectBytecodeInterface::writeAttribute(
     Attribute attr, DialectBytecodeWriter &writer) const {
   return TypeSwitch<Attribute, LogicalResult>(attr)
       .Case<ArrayAttr, DictionaryAttr, FloatAttr, IntegerAttr, StringAttr,
-            SymbolRefAttr, TypeAttr>([&](auto attr) {
+            SymbolRefAttr, TypeAttr, CallSiteLoc, FileLineColLoc, FusedLoc,
+            NameLoc>([&](auto attr) {
         write(attr, writer);
         return success();
       })
+      .Case([&](OpaqueLoc attr) { return write(attr, writer); })
       .Case([&](UnitAttr) {
         writer.writeVarInt(builtin_encoding::kUnitAttr);
         return success();
       })
+      .Case([&](UnknownLoc) {
+        writer.writeVarInt(builtin_encoding::kUnknownLoc);
+        return success();
+      })
       .Default([&](Attribute) { return failure(); });
 }
 
@@ -370,6 +486,48 @@ void BuiltinDialectBytecodeInterface::write(
   writer.writeType(attr.getValue());
 }
 
+void BuiltinDialectBytecodeInterface::write(
+    CallSiteLoc attr, DialectBytecodeWriter &writer) const {
+  writer.writeVarInt(builtin_encoding::kCallSiteLoc);
+  writer.writeAttribute(attr.getCallee());
+  writer.writeAttribute(attr.getCaller());
+}
+
+void BuiltinDialectBytecodeInterface::write(
+    FileLineColLoc attr, DialectBytecodeWriter &writer) const {
+  writer.writeVarInt(builtin_encoding::kFileLineColLoc);
+  writer.writeAttribute(attr.getFilename());
+  writer.writeVarInt(attr.getLine());
+  writer.writeVarInt(attr.getColumn());
+}
+
+void BuiltinDialectBytecodeInterface::write(
+    FusedLoc attr, DialectBytecodeWriter &writer) const {
+  if (Attribute metadata = attr.getMetadata()) {
+    writer.writeVarInt(builtin_encoding::kFusedLocWithMetadata);
+    writer.writeAttributes(attr.getLocations());
+    writer.writeAttribute(metadata);
+  } else {
+    writer.writeVarInt(builtin_encoding::kFusedLoc);
+    writer.writeAttributes(attr.getLocations());
+  }
+}
+
+void BuiltinDialectBytecodeInterface::write(
+    NameLoc attr, DialectBytecodeWriter &writer) const {
+  writer.writeVarInt(builtin_encoding::kNameLoc);
+  writer.writeAttribute(attr.getName());
+  writer.writeAttribute(attr.getChildLoc());
+}
+
+LogicalResult
+BuiltinDialectBytecodeInterface::write(OpaqueLoc attr,
+                                       DialectBytecodeWriter &writer) const {
+  // We can't encode an OpaqueLoc directly given that it is in-memory only, so
+  // encode the fallback instead.
+  return writeAttribute(attr.getFallbackLocation(), writer);
+}
+
 //===----------------------------------------------------------------------===//
 // Types: Reader
 

diff  --git a/mlir/test/Dialect/Builtin/Bytecode/attrs.mlir b/mlir/test/Dialect/Builtin/Bytecode/attrs.mlir
index c309b85e0ce29..f865ff36b5060 100644
--- a/mlir/test/Dialect/Builtin/Bytecode/attrs.mlir
+++ b/mlir/test/Dialect/Builtin/Bytecode/attrs.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt -emit-bytecode %s | mlir-opt | FileCheck %s
+// RUN: mlir-opt -emit-bytecode %s | mlir-opt -mlir-print-local-scope | FileCheck %s
 
 // Bytecode currently does not support big-endian platforms
 // UNSUPPORTED: s390x-
@@ -78,3 +78,57 @@ module @TestType attributes {
   // CHECK: bytecode.type = i178
   bytecode.type = i178
 } {}
+
+//===----------------------------------------------------------------------===//
+// CallSiteLoc
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @TestLocCallSite
+module @TestLocCallSite attributes {
+  // CHECK: bytecode.loc = loc(callsite("foo" at "mysource.cc":10:8))
+  bytecode.loc = loc(callsite("foo" at "mysource.cc":10:8))
+} {}
+
+//===----------------------------------------------------------------------===//
+// FileLineColLoc
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @TestLocFileLineCol
+module @TestLocFileLineCol attributes {
+  // CHECK: bytecode.loc = loc("mysource.cc":10:8)
+  bytecode.loc = loc("mysource.cc":10:8)
+} {}
+
+//===----------------------------------------------------------------------===//
+// FusedLoc
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @TestLocFused
+module @TestLocFused attributes {
+  // CHECK: bytecode.loc = loc(fused["foo", "mysource.cc":10:8])
+  // CHECK: bytecode.loc2 = loc(fused<"myPass">["foo", "foo2"])
+  bytecode.loc = loc(fused["foo", "mysource.cc":10:8]),
+  bytecode.loc2 = loc(fused<"myPass">["foo", "foo2"])
+} {}
+
+//===----------------------------------------------------------------------===//
+// NameLoc
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @TestLocName
+module @TestLocName attributes {
+  // CHECK: bytecode.loc = loc("foo")
+  // CHECK: bytecode.loc2 = loc("foo"("mysource.cc":10:8))
+  bytecode.loc = loc("foo"),
+  bytecode.loc2 = loc("foo"("mysource.cc":10:8))
+} {}
+
+//===----------------------------------------------------------------------===//
+// UnknownLoc
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @TestLocUnknown
+module @TestLocUnknown attributes {
+  // CHECK: bytecode.loc = loc(unknown)
+  bytecode.loc = loc(unknown)
+} {}


        


More information about the Mlir-commits mailing list