forked from iree-org/iree
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Codegen] Replace LICM with a version that checks trip count (iree-or…
…g#18679) The upstream LICM pass does no verification that it is safe to hoist an op out of a loop (i.e. the loop has >= 1 trip count). This replaces all uses of LICM in codegen with a version that does this verification. Other phases of the compiler probably should switch as well.
- Loading branch information
Showing
13 changed files
with
213 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
compiler/src/iree/compiler/Codegen/Common/IREELoopInvariantCodeMotion.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2024 The IREE Authors | ||
// | ||
// Licensed 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 "iree/compiler/Codegen/Common/Passes.h" | ||
#include "iree/compiler/Codegen/Transforms/Transforms.h" | ||
|
||
namespace mlir::iree_compiler { | ||
|
||
#define GEN_PASS_DEF_IREELOOPINVARIANTCODEMOTIONPASS | ||
#include "iree/compiler/Codegen/Common/Passes.h.inc" | ||
|
||
namespace { | ||
/// IREE loop invariant code motion (LICM) pass. | ||
struct IREELoopInvariantCodeMotionPass | ||
: public impl::IREELoopInvariantCodeMotionPassBase< | ||
IREELoopInvariantCodeMotionPass> { | ||
void runOnOperation() override; | ||
}; | ||
} // namespace | ||
|
||
void IREELoopInvariantCodeMotionPass::runOnOperation() { | ||
moveLoopInvariantCodeFromGuaranteedLoops(getOperation()); | ||
} | ||
|
||
} // namespace mlir::iree_compiler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
compiler/src/iree/compiler/Codegen/Common/test/iree_loop_invariant_code_motion.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// RUN: iree-opt %s -split-input-file --iree-loop-invariant-code-motion | FileCheck %s | ||
|
||
func.func @nested_loops_code_invariant_to_both() { | ||
%m = memref.alloc() : memref<10xf32> | ||
%cf7 = arith.constant 7.0 : f32 | ||
%cf8 = arith.constant 8.0 : f32 | ||
|
||
affine.for %arg0 = 0 to 10 { | ||
affine.for %arg1 = 0 to 10 { | ||
%v0 = arith.addf %cf7, %cf8 : f32 | ||
} | ||
} | ||
return | ||
} | ||
|
||
// CHECK-LABEL: @nested_loops_code_invariant_to_both | ||
// CHECK: memref.alloc() : memref<10xf32> | ||
// CHECK-NEXT: arith.constant 7 | ||
// CHECK-NEXT: arith.constant 8 | ||
// CHECK-NEXT: arith.addf | ||
|
||
// ----- | ||
|
||
func.func @do_not_hoist_with_unknown_trip_count(%lb: index, %ub: index) { | ||
affine.for %arg1 = %lb to %ub { | ||
affine.for %arg0 = 0 to 10 { | ||
} | ||
} | ||
return | ||
} | ||
|
||
// CHECK-LABEL: @do_not_hoist_with_unknown_trip_count | ||
// CHECK-NEXT: affine.for | ||
// CHECK-NEXT: affine.for | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: } | ||
|
||
// ----- | ||
|
||
func.func @do_not_hoist_scf_for_with_unknown_trip_count(%lb: index, %ub: index) { | ||
%c1 = arith.constant 1 : index | ||
%cf7 = arith.constant 7.0 : f32 | ||
%cf8 = arith.constant 8.0 : f32 | ||
scf.for %arg0 = %lb to %ub step %c1 { | ||
%v0 = arith.addf %cf7, %cf8 : f32 | ||
} | ||
return | ||
} | ||
|
||
// CHECK-LABEL: @do_not_hoist_scf_for_with_unknown_trip_count | ||
// CHECK: scf.for | ||
// CHECK-NEXT: arith.addf | ||
// CHECK-NEXT: } | ||
|
||
// ----- | ||
|
||
func.func @do_hoist_scf_for_with_known_trip_count() { | ||
%c4 = arith.constant 4 : index | ||
%c6 = arith.constant 6 : index | ||
%c1 = arith.constant 1 : index | ||
%cf7 = arith.constant 7.0 : f32 | ||
%cf8 = arith.constant 8.0 : f32 | ||
scf.for %arg0 = %c4 to %c6 step %c1 { | ||
%v0 = arith.addf %cf7, %cf8 : f32 | ||
} | ||
return | ||
} | ||
|
||
// CHECK-LABEL: @do_hoist_scf_for_with_known_trip_count | ||
// CHECK: arith.addf | ||
// CHECK: scf.for | ||
|
||
// ----- | ||
|
||
func.func @do_not_hoist_scf_while() { | ||
%c4 = arith.constant 4 : index | ||
%c0 = arith.constant 0 : index | ||
%cf7 = arith.constant 7.0 : f32 | ||
%cf8 = arith.constant 8.0 : f32 | ||
scf.while (%iter = %c0) : (index) -> (index) { | ||
%cond = arith.cmpi slt, %iter, %c4 : index | ||
scf.condition(%cond) %iter : index | ||
} do { | ||
^bb0(%arg1: index): | ||
%v0 = arith.addf %cf7, %cf8 : f32 | ||
scf.yield %arg1 : index | ||
} | ||
return | ||
} | ||
|
||
// CHECK-LABEL: @do_not_hoist_scf_while | ||
// CHECK: scf.while | ||
// CHECK: scf.condition | ||
// CHECK: arith.addf | ||
// CHECK: scf.yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.