-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangxinyu
committed
Jun 23, 2024
1 parent
0f2d6eb
commit 298432c
Showing
8 changed files
with
163 additions
and
20 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
34 changes: 34 additions & 0 deletions
34
compiler/include/byteir/Dialect/GPU/Transforms/LegalizeGPULaunch.h
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,34 @@ | ||
//===- LegalizeGPULaunch.h ---------------------------------*--- C++ -*-===// | ||
// | ||
// Copyright 2024 ByteDance Ltd. and/or its affiliates. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef BYTEIR_DIALECT_GPU_TRANSFORMS_LEGALIZEGPULAUNCH_H | ||
#define BYTEIR_DIALECT_GPU_TRANSFORMS_LEGALIZEGPULAUNCH_H | ||
|
||
#include "mlir/Pass/Pass.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include <memory> | ||
|
||
namespace mlir { | ||
namespace func { | ||
class FuncOp; | ||
} // namespace func | ||
|
||
std::unique_ptr<OperationPass<func::FuncOp>> createLegalizeGPULaunchPass(); | ||
|
||
} // namespace mlir | ||
|
||
#endif // BYTEIR_DIALECT_GPU_TRANSFORMS_LEGALIZEGPULAUNCH_H |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//===- LegalizeGPULaunch.cpp-*-===// | ||
// | ||
// Copyright 2022 ByteDance Ltd. and/or its affiliates. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "byteir/Dialect/GPU/Transforms/LegalizeGPULaunch.h" | ||
#include "byteir/Dialect/GPU/Transforms/Utils.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/Dialect/GPU/IR/GPUDialect.h" | ||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/IR/BuiltinOps.h" | ||
#include "mlir/IR/Visitors.h" | ||
#include <string> | ||
|
||
#include "PassDetail.h" | ||
|
||
using namespace llvm; | ||
using namespace mlir; | ||
|
||
namespace { | ||
|
||
static int64_t getSharedMemorySizeInGPULaunch(gpu::LaunchOp op) { | ||
int64_t sharedMemSizeInBytes = 0; | ||
op->walk([&](memref::AllocaOp allocaOp) { | ||
sharedMemSizeInBytes += | ||
allocaOp.getType().getNumElements() * | ||
allocaOp.getType().getElementType().getIntOrFloatBitWidth() / 8; | ||
}); | ||
op->walk([&](memref::AllocOp allocOp) { | ||
sharedMemSizeInBytes += | ||
allocOp.getType().getNumElements() * | ||
allocOp.getType().getElementType().getIntOrFloatBitWidth() / 8; | ||
}); | ||
return sharedMemSizeInBytes; | ||
} | ||
|
||
struct LegalizeGPULaunchPass | ||
: public LegalizeGPULaunchBase<LegalizeGPULaunchPass> { | ||
LegalizeGPULaunchPass() : LegalizeGPULaunchBase<LegalizeGPULaunchPass>() {} | ||
void runOnOperation() override { | ||
func::FuncOp funcOp = getOperation(); | ||
OpBuilder builder(funcOp.getContext()); | ||
auto launchOps = funcOp.getOps<gpu::LaunchOp>(); | ||
for (auto launchOp : launchOps) { | ||
int64_t sharedMemSize = getSharedMemorySizeInGPULaunch(launchOp); | ||
if (sharedMemSize < 48 * 1024) // 48kB | ||
continue; | ||
builder.setInsertionPoint(launchOp); | ||
Value sharedMemSizeValue = builder.create<arith::ConstantOp>( | ||
launchOp.getLoc(), builder.getI32IntegerAttr(sharedMemSize)); | ||
if (!launchOp.getDynamicSharedMemorySizeMutable().empty()) { | ||
continue; | ||
} | ||
launchOp.getDynamicSharedMemorySizeMutable().append( | ||
ValueRange{sharedMemSizeValue}); | ||
} | ||
} | ||
}; | ||
} // namespace | ||
|
||
std::unique_ptr<OperationPass<func::FuncOp>> | ||
mlir::createLegalizeGPULaunchPass() { | ||
return std::make_unique<LegalizeGPULaunchPass>(); | ||
} |
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