Skip to content

Commit 4648fe1

Browse files
[mlir] Add loop annotation attribute interface
Interface used to propagate loop annotation attributes between passes that shouldn't need to rely on the dialects they come from.
1 parent 00cad3e commit 4648fe1

File tree

11 files changed

+81
-9
lines changed

11 files changed

+81
-9
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVMIR_ATTRDEFS
1010
#define LLVMIR_ATTRDEFS
1111

12+
include "mlir/Interfaces/LoopAnnotationAttrInterface.td"
1213
include "mlir/Dialect/LLVMIR/LLVMDialect.td"
1314
include "mlir/Dialect/LLVMIR/LLVMInterfaces.td"
1415
include "mlir/IR/AttrTypeBase.td"
@@ -204,7 +205,7 @@ def LoopUnswitchAttr : LLVM_Attr<"LoopUnswitch", "loop_unswitch"> {
204205
let assemblyFormat = "`<` struct(params) `>`";
205206
}
206207

207-
def LoopAnnotationAttr : LLVM_Attr<"LoopAnnotation", "loop_annotation"> {
208+
def LoopAnnotationAttr : LLVM_Attr<"LoopAnnotation", "loop_annotation", [LoopAnnotationAttrInterface]> {
208209
let description = [{
209210
This attributes encapsulates "loop metadata". It is meant to decorate
210211
branches that are "latches" (loop backedges) and maps to the `!llvm.loop`

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
1818
#include "mlir/IR/OpImplementation.h"
19+
#include "mlir/Interfaces/LoopAnnotationAttrInterface.h"
1920
#include <optional>
2021

2122
#include "mlir/Dialect/LLVMIR/LLVMOpsEnums.h.inc"

mlir/include/mlir/Interfaces/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ mlir_tablegen(MemorySlotTypeInterfaces.cpp.inc -gen-type-interface-defs)
2626
add_public_tablegen_target(MLIRMemorySlotInterfacesIncGen)
2727
add_dependencies(mlir-generic-headers MLIRMemorySlotInterfacesIncGen)
2828

29+
set(LLVM_TARGET_DEFINITIONS LoopAnnotationAttrInterface.td)
30+
mlir_tablegen(LoopAnnotationAttrInterface.h.inc -gen-attr-interface-decls)
31+
mlir_tablegen(LoopAnnotationAttrInterface.cpp.inc -gen-attr-interface-defs)
32+
add_public_tablegen_target(MLIRLoopAnnotationAttrInterfaceIncGen)
33+
add_dependencies(mlir-generic-headers MLIRLoopAnnotationAttrInterfaceIncGen)
34+
2935
set(LLVM_TARGET_DEFINITIONS DataLayoutInterfaces.td)
3036
mlir_tablegen(DataLayoutAttrInterface.h.inc -gen-attr-interface-decls)
3137
mlir_tablegen(DataLayoutAttrInterface.cpp.inc -gen-attr-interface-defs)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef MLIR_INTERFACES_LOOPANNOTATIONINTERFACE_H
2+
#define MLIR_INTERFACES_LOOPANNOTATIONINTERFACE_H
3+
4+
#include "mlir/IR/Attributes.h"
5+
#include "mlir/Interfaces/LoopAnnotationAttrInterface.h.inc"
6+
7+
#endif // MLIR_INTERFACES_LOOPANNOTATIONINTERFACE_H
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===- LoopAnnotationAttrInterface.td ----------------------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Defines the interface for attributes on loop-like operations.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_INTERFACES_LOOPANNOTATIONATTRINTERFACE
14+
#define MLIR_INTERFACES_LOOPANNOTATIONATTRINTERFACE
15+
16+
include "mlir/IR/OpBase.td"
17+
18+
def LoopAnnotationAttrInterface : AttrInterface<"LoopAnnotationAttrInterface"> {
19+
let description = [{
20+
Metadata that should live on loop-like operations.
21+
}];
22+
let cppNamespace = "::mlir";
23+
24+
let methods = [
25+
];
26+
}
27+
28+
#endif // MLIR_INTERFACES_LOOPANNOTATIONATTRINTERFACE

mlir/include/mlir/Interfaces/LoopLikeInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define MLIR_INTERFACES_LOOPLIKEINTERFACE_H_
1515

1616
#include "mlir/IR/OpDefinition.h"
17+
#include "mlir/Interfaces/LoopAnnotationAttrInterface.h"
1718

1819
namespace mlir {
1920
class RewriterBase;

mlir/include/mlir/Interfaces/LoopLikeInterface.td

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define MLIR_INTERFACES_LOOPLIKEINTERFACE
1515

1616
include "mlir/IR/OpBase.td"
17+
include "mlir/Interfaces/LoopAnnotationAttrInterface.td"
1718

1819
//===----------------------------------------------------------------------===//
1920
// Interfaces
@@ -232,6 +233,22 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
232233
/*defaultImplementation=*/[{
233234
return ::mlir::failure();
234235
}]
236+
>,
237+
InterfaceMethod<[{
238+
Returns the loop annotation attributes.
239+
}],
240+
/*retTy=*/"::mlir::SmallVector<::mlir::NamedAttribute>",
241+
/*methodName=*/"getLoopAnnotationAttributes",
242+
/*args=*/(ins),
243+
/*methodBody=*/"",
244+
/*defaultImplementation=*/[{
245+
SmallVector<NamedAttribute> loopAnnotationAttrs;
246+
llvm::copy_if($_op->getAttrs(), std::back_inserter(loopAnnotationAttrs),
247+
[](auto attr) -> bool {
248+
return mlir::isa<mlir::LoopAnnotationAttrInterface>(attr.getValue());
249+
});
250+
return loopAnnotationAttrs;
251+
}]
235252
>
236253
];
237254

mlir/lib/Conversion/SCFToControlFlow/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ add_mlir_conversion_library(MLIRSCFToControlFlow
1010
LINK_LIBS PUBLIC
1111
MLIRArithDialect
1212
MLIRControlFlowDialect
13-
MLIRLLVMDialect
1413
MLIRSCFDialect
1514
MLIRSCFTransforms
1615
MLIRTransforms

mlir/lib/Conversion/SCFToControlFlow/SCFToControlFlow.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include "mlir/Dialect/Arith/IR/Arith.h"
1717
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
18-
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1918
#include "mlir/Dialect/SCF/IR/SCF.h"
2019
#include "mlir/Dialect/SCF/Transforms/Transforms.h"
2120
#include "mlir/IR/Builders.h"
@@ -377,12 +376,8 @@ LogicalResult ForLowering::matchAndRewrite(ForOp forOp,
377376

378377
// Let the CondBranchOp carry the LLVM attributes from the ForOp, such as the
379378
// llvm.loop_annotation attribute.
380-
SmallVector<NamedAttribute> llvmAttrs;
381-
llvm::copy_if(forOp->getAttrs(), std::back_inserter(llvmAttrs),
382-
[](auto attr) {
383-
return isa<LLVM::LLVMDialect>(attr.getValue().getDialect());
384-
});
385-
condBranchOp->setDiscardableAttrs(llvmAttrs);
379+
condBranchOp->setDiscardableAttrs(forOp.getLoopAnnotationAttributes());
380+
386381
// The result of the loop operation is the values of the condition block
387382
// arguments except the induction variable on the last iteration.
388383
rewriter.replaceOp(forOp, conditionBlock->getArguments().drop_front());

mlir/lib/Interfaces/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(LLVM_OPTIONAL_SOURCES
1010
FunctionInterfaces.cpp
1111
InferIntRangeInterface.cpp
1212
InferTypeOpInterface.cpp
13+
LoopAnnotationAttrInterface.cpp
1314
LoopLikeInterface.cpp
1415
MemorySlotInterfaces.cpp
1516
ParallelCombiningOpInterface.cpp
@@ -65,6 +66,19 @@ add_mlir_library(MLIRFunctionInterfaces
6566
add_mlir_interface_library(InferIntRangeInterface)
6667
add_mlir_interface_library(InferTypeOpInterface)
6768

69+
add_mlir_library(MLIRLoopAnnotationAttrInterface
70+
LoopAnnotationAttrInterface.cpp
71+
72+
ADDITIONAL_HEADER_DIRS
73+
${MLIR_MAIN_INCLUDE_DIR}/mlir/Interfaces
74+
75+
DEPENDS
76+
MLIRLoopAnnotationAttrInterfaceIncGen
77+
78+
LINK_LIBS PUBLIC
79+
MLIRIR
80+
)
81+
6882
add_mlir_library(MLIRLoopLikeInterface
6983
LoopLikeInterface.cpp
7084

0 commit comments

Comments
 (0)