[llvm] [offload][SYCL] Add Module splitting by categories. (PR #131347)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 21 08:22:49 PDT 2025
================
@@ -70,6 +77,163 @@ static cl::opt<std::string>
MCPU("mcpu", cl::desc("Target CPU, ignored if --mtriple is not used"),
cl::value_desc("cpu"), cl::cat(SplitCategory));
+enum class SplitByCategoryType {
+ SBCT_ByModuleId,
+ SBCT_ByKernel,
+ SBCT_None,
+};
+
+static cl::opt<SplitByCategoryType> SplitByCategory(
+ "split-by-category",
+ cl::desc("Split by category. If present, splitting by category is used "
+ "with the specified categorization type."),
+ cl::Optional, cl::init(SplitByCategoryType::SBCT_None),
+ cl::values(clEnumValN(SplitByCategoryType::SBCT_ByModuleId, "module-id",
+ "one output module per translation unit marked with "
+ "\"module-id\" attribute"),
+ clEnumValN(SplitByCategoryType::SBCT_ByKernel, "kernel",
+ "one output module per kernel")),
+ cl::cat(SplitCategory));
+
+static cl::opt<bool> OutputAssembly{
+ "S", cl::desc("Write output as LLVM assembly"), cl::cat(SplitCategory)};
+
+void writeStringToFile(StringRef Content, StringRef Path) {
+ std::error_code EC;
+ raw_fd_ostream OS(Path, EC);
+ if (EC) {
+ errs() << formatv("error opening file: {0}, error: {1}\n", Path,
+ EC.message());
+ exit(1);
+ }
+
+ OS << Content << "\n";
+}
+
+void writeModuleToFile(const Module &M, StringRef Path, bool OutputAssembly) {
+ int FD = -1;
+ if (std::error_code EC = sys::fs::openFileForWrite(Path, FD)) {
+ errs() << formatv("error opening file: {0}, error: {1}", Path, EC.message())
+ << '\n';
+ exit(1);
+ }
+
+ raw_fd_ostream OS(FD, /*ShouldClose*/ true);
+ if (OutputAssembly)
+ M.print(OS, /*AssemblyAnnotationWriter*/ nullptr);
+ else
+ WriteBitcodeToFile(M, OS);
+}
+
+/// FunctionCategorizer is used for splitting by category either by module-id or
+/// by kernels. It doesn't provide categories for functions other than kernels.
+/// Categorizer computes a string key for the given Function and records the
+/// association between the string key and an integer category. If a string key
+/// is already belongs to some category than the corresponding integer category
+/// is returned.
+class FunctionCategorizer {
+public:
+ FunctionCategorizer(SplitByCategoryType Type) : Type(Type) {}
+
+ FunctionCategorizer() = delete;
+ FunctionCategorizer(FunctionCategorizer &) = delete;
+ FunctionCategorizer &operator=(const FunctionCategorizer &) = delete;
+ FunctionCategorizer(FunctionCategorizer &&) = default;
+ FunctionCategorizer &operator=(FunctionCategorizer &&) = default;
+
+ /// Returns integer specifying the category for the given \p F.
+ /// If the given function isn't a kernel then returns std::nullopt.
+ std::optional<int> operator()(const Function &F) {
+ if (!isEntryPoint(F))
+ return std::nullopt; // skip the function.
+
+ auto StringKey = computeFunctionCategory(Type, F);
----------------
shiltian wrote:
no auto here
https://github.com/llvm/llvm-project/pull/131347
More information about the llvm-commits
mailing list