-
Notifications
You must be signed in to change notification settings - Fork 788
[SYCL][NFC] Extract specialization constant's processing from sycl-post-link to SYCLPostLink library. #19022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: sycl
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//= SpecializationConstants.h - Processing of SYCL Specialization Constants ==// | ||
// | ||
// Part of the LLVM Project, 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// Specialization constants processing consists of lowering and generation | ||
// of new module with spec consts replaced by default values. | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_SYCL_POST_LINK_SPECIALIZATION_CONSTANTS_H | ||
#define LLVM_SYCL_POST_LINK_SPECIALIZATION_CONSTANTS_H | ||
|
||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/SYCLLowerIR/SpecConstants.h" | ||
#include "llvm/SYCLPostLink/ModuleSplitter.h" | ||
|
||
#include <optional> | ||
|
||
namespace llvm { | ||
namespace sycl { | ||
|
||
/// Handling consists of SpecConsts's lowering depending on the given | ||
/// \p Mode. If \p Mode is std::nullopt, then no lowering happens. | ||
/// If \p GenerateModuleDescWithDefaultSpecConsts is true, then a generation | ||
/// of new modules with specialization constants replaced by default values | ||
/// happens and the result is written in \p NewModuleDescs. | ||
/// Otherwise, \p NewModuleDescs is expected to be nullptr. | ||
/// | ||
/// Returns boolean value indicating whether the lowering has changed the input | ||
/// modules. | ||
bool handleSpecializationConstants( | ||
llvm::SmallVectorImpl<module_split::ModuleDesc> &MDs, | ||
std::optional<SpecConstantsPass::HandlingMode> Mode, | ||
bool GenerateModuleDescWithDefaultSpecConsts = false, | ||
llvm::SmallVectorImpl<module_split::ModuleDesc> *NewModuleDescs = nullptr); | ||
|
||
} // namespace sycl | ||
} // namespace llvm | ||
|
||
#endif // LLVM_SYCL_POST_LINK_SPECIALIZATION_CONSTANTS_H |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,97 @@ | ||||||||||||
//= SpecializationConstants.h - Processing of SYCL Specialization Constants ==// | ||||||||||||
// | ||||||||||||
// Part of the LLVM Project, 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 | ||||||||||||
// | ||||||||||||
//===----------------------------------------------------------------------===// | ||||||||||||
// See comments in the header. | ||||||||||||
//===----------------------------------------------------------------------===// | ||||||||||||
|
||||||||||||
#include "llvm/SYCLPostLink/SpecializationConstants.h" | ||||||||||||
|
||||||||||||
#include "llvm/ADT/SmallVector.h" | ||||||||||||
#include "llvm/IR/Function.h" | ||||||||||||
#include "llvm/IR/Module.h" | ||||||||||||
#include "llvm/IR/PassManager.h" | ||||||||||||
#include "llvm/Passes/PassBuilder.h" | ||||||||||||
#include "llvm/SYCLLowerIR/SpecConstants.h" | ||||||||||||
#include "llvm/SYCLPostLink/ModuleSplitter.h" | ||||||||||||
#include "llvm/Transforms/IPO/StripDeadPrototypes.h" | ||||||||||||
|
||||||||||||
#include <optional> | ||||||||||||
|
||||||||||||
using namespace llvm; | ||||||||||||
|
||||||||||||
namespace { | ||||||||||||
|
||||||||||||
bool lowerSpecConstants(module_split::ModuleDesc &MD, | ||||||||||||
SpecConstantsPass::HandlingMode Mode) { | ||||||||||||
ModulePassManager RunSpecConst; | ||||||||||||
ModuleAnalysisManager MAM; | ||||||||||||
SpecConstantsPass SCP(Mode); | ||||||||||||
// Register required analysis | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
MAM.registerPass([&] { return PassInstrumentationAnalysis(); }); | ||||||||||||
RunSpecConst.addPass(std::move(SCP)); | ||||||||||||
|
||||||||||||
// Perform the spec constant intrinsics transformation on resulting module | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
PreservedAnalyses Res = RunSpecConst.run(MD.getModule(), MAM); | ||||||||||||
MD.Props.SpecConstsMet = !Res.areAllPreserved(); | ||||||||||||
return MD.Props.SpecConstsMet; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/// Function generates the copy of the given \p MD where all uses of | ||||||||||||
/// Specialization Constants are replaced by corresponding default values. | ||||||||||||
/// If the Module in \p MD doesn't contain specialization constants then | ||||||||||||
/// std::nullopt is returned. | ||||||||||||
std::optional<module_split::ModuleDesc> | ||||||||||||
cloneModuleWithSpecConstsReplacedByDefaultValues( | ||||||||||||
const module_split::ModuleDesc &MD) { | ||||||||||||
std::optional<module_split::ModuleDesc> NewMD; | ||||||||||||
if (!checkModuleContainsSpecConsts(MD.getModule())) | ||||||||||||
return NewMD; | ||||||||||||
|
||||||||||||
NewMD = MD.clone(); | ||||||||||||
NewMD->setSpecConstantDefault(true); | ||||||||||||
|
||||||||||||
ModulePassManager MPM; | ||||||||||||
ModuleAnalysisManager MAM; | ||||||||||||
SpecConstantsPass SCP(SpecConstantsPass::HandlingMode::default_values); | ||||||||||||
MAM.registerPass([&] { return PassInstrumentationAnalysis(); }); | ||||||||||||
MPM.addPass(std::move(SCP)); | ||||||||||||
MPM.addPass(StripDeadPrototypesPass()); | ||||||||||||
|
||||||||||||
PreservedAnalyses Res = MPM.run(NewMD->getModule(), MAM); | ||||||||||||
NewMD->Props.SpecConstsMet = !Res.areAllPreserved(); | ||||||||||||
assert(NewMD->Props.SpecConstsMet && | ||||||||||||
"This property should be true since the presence of SpecConsts " | ||||||||||||
"has been checked before the run of the pass"); | ||||||||||||
NewMD->rebuildEntryPoints(); | ||||||||||||
return NewMD; | ||||||||||||
} | ||||||||||||
|
||||||||||||
} // namespace | ||||||||||||
|
||||||||||||
bool llvm::sycl::handleSpecializationConstants( | ||||||||||||
SmallVectorImpl<module_split::ModuleDesc> &MDs, | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
std::optional<SpecConstantsPass::HandlingMode> Mode, | ||||||||||||
bool GenerateModuleDescWithDefaultSpecConsts, | ||||||||||||
SmallVectorImpl<module_split::ModuleDesc> *NewModuleDescs) { | ||||||||||||
assert((GenerateModuleDescWithDefaultSpecConsts ^ !NewModuleDescs) && | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is quite difficult to read. Could we rewrite this to something simpler?
Suggested change
|
||||||||||||
"NewModuleDescs pointer is nullptr iff " | ||||||||||||
"GenerateModuleDescWithDefaultSpecConsts is false."); | ||||||||||||
|
||||||||||||
bool Modified = false; | ||||||||||||
for (module_split::ModuleDesc &MD : MDs) { | ||||||||||||
if (GenerateModuleDescWithDefaultSpecConsts) | ||||||||||||
if (std::optional<module_split::ModuleDesc> NewMD = | ||||||||||||
cloneModuleWithSpecConstsReplacedByDefaultValues(MD); | ||||||||||||
NewMD) | ||||||||||||
NewModuleDescs->push_back(std::move(*NewMD)); | ||||||||||||
|
||||||||||||
if (Mode) | ||||||||||||
Modified |= lowerSpecConstants(MD, *Mode); | ||||||||||||
} | ||||||||||||
|
||||||||||||
return Modified; | ||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.