@@ -60,67 +60,20 @@ SPDX-License-Identifier: MIT
6060#include " llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
6161#include " llvm/Transforms/Utils.h"
6262#include " llvmWrapper/IR/Instructions.h"
63- #include " llvm/Support/YAMLTraits.h"
64- #include " llvmWrapper/Support/YAMLParser.h"
6563#include " common/LLVMWarningsPop.hpp"
6664
65+ #include " shared.h"
66+
6767#include < map>
6868#include < optional>
6969
7070using namespace llvm ;
7171
72- // Address space descriptor from YAML
73- struct AddressSpaceDesc {
74- std::string name;
75- std::string description;
76- bool constant = false ;
77- unsigned AS = 0 ; // Will be auto-assigned based on order
78- };
79-
80- constexpr unsigned FIRST_ADDRSPACE = 100 ;
81-
82- // YAML parsing traits
83- namespace llvm {
84- namespace yaml {
85- template <> struct MappingTraits <AddressSpaceDesc> {
86- static void mapping (IO &io, AddressSpaceDesc &desc) {
87- io.mapRequired (" name" , desc.name );
88- io.mapRequired (" description" , desc.description );
89- io.mapRequired (" constant" , desc.constant );
90- }
91- };
92- } // namespace yaml
93- } // namespace llvm
94-
95- LLVM_YAML_IS_SEQUENCE_VECTOR (AddressSpaceDesc)
96-
97- struct AddressSpaceConfig {
98- std::vector<AddressSpaceDesc> address_spaces;
99- };
100-
101- namespace llvm {
102- namespace yaml {
103- template <> struct MappingTraits <AddressSpaceConfig> {
104- static void mapping (IO &io, AddressSpaceConfig &config) { io.mapRequired (" address_spaces" , config.address_spaces ); }
105- };
106- } // namespace yaml
107- } // namespace llvm
108-
109- // Global storage for address space info loaded from YAML
110- static std::vector<AddressSpaceDesc> LoadedAddrspaces;
111-
11272static cl::opt<std::string> InputFilename (cl::Positional, cl::desc(" <input bitcode file>" ), cl::value_desc(" filename" ),
113- cl::init( " " ) );
73+ cl::Required );
11474
11575static cl::opt<std::string> OutputFilename (cl::Positional, cl::desc(" <output header file>" ), cl::value_desc(" header" ),
116- cl::init(" " ));
117-
118- static cl::opt<std::string> YamlPath (" yaml-path" , cl::desc(" Path to address space descriptor YAML file" ),
119- cl::value_desc(" yaml" ), cl::init(" " ));
120-
121- static cl::opt<std::string> GenDescPath (" gen-desc" ,
122- cl::desc (" Generate address space descriptor header at specified path" ),
123- cl::value_desc(" path" ), cl::init(" " ));
76+ cl::Required);
12477
12578static cl::opt<bool > DoMangle (" mangle-names" , cl::desc(" Mangles all strings" ), cl::init(false ));
12679
@@ -129,7 +82,7 @@ enum class FuncScope { PRIVATE, PUBLIC };
12982static cl::opt<FuncScope> AutogenScope (" scope" , cl::desc(" Which functions to process" ),
13083 cl::values(clEnumValN(FuncScope::PRIVATE, " private" , " private header" ),
13184 clEnumValN(FuncScope::PUBLIC, " public" , " public header" )),
132- cl::init(FuncScope::PRIVATE) );
85+ cl::Required );
13386
13487using HoleMap = MapVector<const Type *, std::string>;
13588using AlignMap = DenseMap<const Type *, uint32_t >;
@@ -614,7 +567,7 @@ bool processCreate(const Function &F, raw_ostream &OS, const AnnotationMap &Anno
614567 SymbolTracker SymTracker;
615568 AddrspaceToValueMap AddrMap;
616569 SmallDenseSet<uint32_t > ReservedAddrspaces;
617- for (auto &ASInfo : LoadedAddrspaces )
570+ for (auto &ASInfo : ReservedAS )
618571 ReservedAddrspaces.insert (ASInfo.AS );
619572 for (auto &Arg : F.args ()) {
620573 SymTracker.addArg (&Arg);
@@ -1059,8 +1012,8 @@ bool process(const Module &M, raw_ostream &OS) {
10591012
10601013void markInvariant (Module &M) {
10611014 SmallDenseSet<uint32_t > ConstantAS;
1062- for (auto &ASInfo : LoadedAddrspaces ) {
1063- if (ASInfo.constant )
1015+ for (auto &ASInfo : ReservedAS ) {
1016+ if (ASInfo.Constant )
10641017 ConstantAS.insert (ASInfo.AS );
10651018 }
10661019
@@ -1119,99 +1072,6 @@ void preprocess(Module &M) {
11191072 rewriteAnonTypes (M);
11201073}
11211074
1122- bool loadAddressSpacesFromYAML (const std::string &YamlPath) {
1123- ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = MemoryBuffer::getFile (YamlPath);
1124- if (std::error_code EC = FileOrErr.getError ()) {
1125- errs () << " Error opening YAML file '" << YamlPath << " ': " << EC.message () << " \n " ;
1126- return false ;
1127- }
1128-
1129- yaml::Input YamlInput (FileOrErr.get ()->getBuffer ());
1130- AddressSpaceConfig Config;
1131- YamlInput >> Config;
1132-
1133- if (YamlInput.error ()) {
1134- errs () << " Error parsing YAML file '" << YamlPath << " '\n " ;
1135- return false ;
1136- }
1137-
1138- LoadedAddrspaces.clear ();
1139- for (size_t i = 0 ; i < Config.address_spaces .size (); i++) {
1140- auto &desc = Config.address_spaces [i];
1141- desc.AS = FIRST_ADDRSPACE + static_cast <uint32_t >(i);
1142- LoadedAddrspaces.push_back (desc);
1143- }
1144-
1145- return true ;
1146- }
1147-
1148- bool generateDescriptorHeader (const std::string &YamlPath, const std::string &OutputPath) {
1149- // Load the YAML configuration
1150- ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = MemoryBuffer::getFile (YamlPath);
1151- if (std::error_code EC = FileOrErr.getError ()) {
1152- errs () << " Error opening YAML file '" << YamlPath << " ': " << EC.message () << " \n " ;
1153- return false ;
1154- }
1155-
1156- yaml::Input YamlInput (FileOrErr.get ()->getBuffer ());
1157- AddressSpaceConfig Config;
1158- YamlInput >> Config;
1159-
1160- if (YamlInput.error ()) {
1161- errs () << " Error parsing YAML file '" << YamlPath << " '\n " ;
1162- return false ;
1163- }
1164-
1165- // Open output file
1166- std::error_code EC;
1167- sys::fs::OpenFlags fsFlags = sys::fs::OF_TextWithCRLF;
1168- raw_fd_ostream outfile (OutputPath, EC, fsFlags);
1169-
1170- if (EC) {
1171- errs () << " Couldn't open output file '" << OutputPath << " ' for writing: " << EC.message () << " \n " ;
1172- return false ;
1173- }
1174-
1175- // Write the header
1176- outfile << " // AUTOGENERATED FILE - DO NOT EDIT\n " ;
1177- outfile << " // Generated from: " << YamlPath << " \n\n " ;
1178- outfile << " #pragma once\n\n " ;
1179- outfile << " struct AddrspaceInfo {\n " ;
1180- outfile << " unsigned AS;\n " ;
1181- outfile << " bool Constant;\n " ;
1182- outfile << " constexpr AddrspaceInfo(unsigned AS, bool Constant) : AS(AS), Constant(Constant) {}\n " ;
1183- outfile << " };\n\n " ;
1184- outfile << " constexpr AddrspaceInfo ReservedAS[] = {\n " ;
1185- outfile << " // clang-format off\n " ;
1186-
1187- for (size_t i = 0 ; i < Config.address_spaces .size (); i++) {
1188- auto &desc = Config.address_spaces [i];
1189- unsigned AS = FIRST_ADDRSPACE + static_cast <unsigned >(i);
1190- const char *constant = desc.constant ? " true " : " false" ;
1191- outfile << " AddrspaceInfo{ " << AS << " , " << constant << " }, // " << desc.name << " : " << desc.description
1192- << " \n " ;
1193- }
1194-
1195- outfile << " // clang-format on\n " ;
1196- outfile << " };\n\n " ;
1197-
1198- // Generate address space defines
1199- outfile << " #if defined(__clang__)\n " ;
1200- for (size_t i = 0 ; i < Config.address_spaces .size (); i++) {
1201- auto &desc = Config.address_spaces [i];
1202- outfile << " // " << desc.description << " \n " ;
1203- outfile << " #define " << desc.name << " __attribute__((address_space(ReservedAS[" << i << " ].AS)))\n " ;
1204- }
1205- outfile << " #else\n " ;
1206- for (size_t i = 0 ; i < Config.address_spaces .size (); i++) {
1207- auto &desc = Config.address_spaces [i];
1208- outfile << " #define " << desc.name << " \n " ;
1209- }
1210- outfile << " #endif // __clang__\n " ;
1211-
1212- return true ;
1213- }
1214-
12151075int main (int argc, char **argv) {
12161076 sys::PrintStackTraceOnErrorSignal (argv[0 ]);
12171077 llvm::PrettyStackTraceProgram X (argc, argv);
@@ -1227,29 +1087,6 @@ Generate code for the specified bitcode.
12271087 if (!cl::ParseCommandLineOptions (argc, argv, Overview))
12281088 return 1 ;
12291089
1230- // Handle --gen-desc mode: generate address space descriptor header and exit
1231- if (!GenDescPath.empty ()) {
1232- if (YamlPath.empty ()) {
1233- errs () << " Error: --gen-desc requires --yaml-path to be specified\n " ;
1234- return 1 ;
1235- }
1236- return generateDescriptorHeader (YamlPath, GenDescPath) ? 0 : 1 ;
1237- }
1238-
1239- // Validate required arguments for normal mode
1240- if (InputFilename.empty () || OutputFilename.empty ()) {
1241- errs () << " Error: input bitcode file and output header file are required\n " ;
1242- return 1 ;
1243- }
1244-
1245- // Load address space configuration from YAML if provided
1246- if (!YamlPath.empty ()) {
1247- if (!loadAddressSpacesFromYAML (YamlPath)) {
1248- errs () << " Failed to load address space configuration from: " << YamlPath << " \n " ;
1249- return 1 ;
1250- }
1251- }
1252-
12531090 LLVMContext Context;
12541091 SMDiagnostic Err;
12551092
0 commit comments