-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CPU] Add
contract
fast-math-flag to arith operations (#14551)
This patch adds the `contract` FMF to some arith operations so that they can be folded into an fma instruction. We are doing this by default as we are lowering matmul ops by default to fmas. We will add different fp modes to have more control on fp optimizations depending on the tolerance to fp errors.
- Loading branch information
Showing
10 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
compiler/src/iree/compiler/Codegen/Common/AddFastMathFlags.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,44 @@ | ||
// Copyright 2023 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/PassDetail.h" | ||
#include "iree/compiler/Codegen/Common/Passes.h" | ||
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
|
||
#define DEBUG_TYPE "iree-codegen-add-fast-math-flags" | ||
|
||
using namespace mlir; | ||
using namespace mlir::iree_compiler; | ||
|
||
/// Add `contract` FMF to operations that support it. | ||
static void addContractFMF(Operation *op) { | ||
LLVM::FastmathFlags contract = LLVM::FastmathFlags::contract; | ||
TypeSwitch<Operation *>(op) | ||
.Case<LLVM::FMulOp, LLVM::FAddOp, LLVM::FSubOp, LLVM::FNegOp>( | ||
[&](auto llvmOp) { llvmOp.setFastmathFlags(contract); }); | ||
} | ||
|
||
namespace { | ||
|
||
/// Add the corresponding fast-math flags to operations given a floating-point | ||
/// optimization mode. | ||
// TODO: For now we only allow default flags, such as arithmetic reassociation. | ||
struct AddFastMathFlagsPass | ||
: public AddFastMathFlagsBase<AddFastMathFlagsPass> { | ||
public: | ||
using AddFastMathFlagsBase::AddFastMathFlagsBase; | ||
|
||
void runOnOperation() override { | ||
getOperation()->walk([](Operation *op) { addContractFMF(op); }); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
std::unique_ptr<OperationPass<LLVM::LLVMFuncOp>> | ||
mlir::iree_compiler::createAddFastMathFlagsPass() { | ||
return std::make_unique<AddFastMathFlagsPass>(); | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
compiler/src/iree/compiler/Codegen/Common/test/add_fmfs.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,13 @@ | ||
// RUN: iree-opt -iree-codegen-add-fast-math-flags --split-input-file %s | FileCheck %s | ||
|
||
// LABEL: llvm.func @fmfs | ||
llvm.func @fmfs() -> f32 { | ||
%c3 = llvm.mlir.constant(3.000000e+00 : f32) : f32 | ||
%c6 = llvm.mlir.constant(6.000000e+00 : f32) : f32 | ||
%mul = llvm.fmul %c3, %c3 : f32 | ||
%add = llvm.fadd %c3, %c6 : f32 | ||
llvm.return %add : f32 | ||
} | ||
|
||
// CHECK: llvm.fmul %{{.*}}, %{{.*}} {fastmathFlags = #llvm.fastmath<contract>} : f32 | ||
// CHECK: llvm.fadd %{{.*}}, %{{.*}} {fastmathFlags = #llvm.fastmath<contract>} : f32 |
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