[PATCH] D116464: [SPIRV 5/6] Add LegalizerInfo, InstructionSelector and utilities

Ilia Diachkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 2 14:33:03 PST 2022


iliya-diyachkov added inline comments.


================
Comment at: llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVBaseInfo.h:23
+
+#define MAKE_ENUM(Enum, Var, Val) Var = Val,
+
----------------
rengolin wrote:
> iliya-diyachkov wrote:
> > rengolin wrote:
> > > We generally frown upon pre-processor macros to define code as it can be really hard to understand what's going on, especially during debug.
> > > 
> > > Looking at other targets' `BaseInfo`, none of them are as complicated as this one, so I'm not sure what to say, really.
> > > 
> > > Another problem perhaps is that these macros will be available to any header or source file that include `BaseInfo`, which can be many, and can encourage people using it elsewhere (hopefully, only on this target).
> > Well, I agree that it looks unpleasant and a little confusing and it can complicate debugging.
> > 
> > In fact, these macros define 30 enums representing entities specific to SPIR-V, and name-getters for them, like the following:
> > ```
> > namespace SourceLanguage {
> > enum SourceLanguage : uint32_t {
> >   Unknown = 0,
> >   ESSL = 1,
> >   GLSL = 2,
> >   OpenCL_C = 3,
> >   OpenCL_CPP = 4,
> >   HLSL = 5,
> > };
> > }
> > std::string getSourceLanguageName(SourceLanguage::SourceLanguage e);
> > ```
> > The name getters are declared in SPIRVBaseInfo.cpp.
> > 
> > These enums are widely used in the general SPIRV code and in several passes, and we can't just get rid of them. We can declare them all manually without macros, but this will increase the source code and probably reduce maintainability/extensibility.
> > 
> > Actually these are simplified macros for SPIRV-specific entities. We have several other source files (I have not published them in this series) that use more complex version of these macros, e.g. they include features, extensions and SPIR-V versions specific to each particular enum member. Like this:
> > ```
> >  X(N, StorageUniform16, 4434, {SBBA(16)}, {SPV_16_BIT}, 0x10300, 0)
> > ```
> > It may be convenient for maintainers to have all info about each entity in one place.
> > 
> > I admit that some of these macros can be re-implemented using tablegen, but for the first time I would leave existing approach, perhaps with some improvements.
> > 
> > I see that some of the macro definitions can be moved from SPIRVBaseInfo.h to SPIRVBaseInfo.cpp so they will not affect other sources that include SPIRVBaseInfo.h.
> > Also (if it helps somehow) we could separate this BaseInfo* to LLVMSPIRVUtils lib, as it's done in AArch64, AMDGPU, ARM and WebAssembly targets.
> > 
> That is certainly the right place for those enums, just not defined via complex chain of macros.
> 
> If you have more of them, and even more complex, downstream, it would certainly be the right time now to move them to TableGen, otherwise it will be harder and harder to get rid of them in some complex refactoring pass.
> 
> This is one of the crucial parts of the first review, to make sure the code doesn't have "contentious issues". Macros in this way are a good candidate.
> 
> When the debugger hits that line, it will point to the macro definition, and will provide no recourse to looking at their definitions or expanding them. Worse, if we start using macros in other macros, the compiler will have a hard time telling the developers what went wrong in the generated code.
> 
> If you look around the code you'll notice that most macros are either silly things or around table-generated stuff. Table gen isn't perfect, but both debuggers and compilers can point at the generated files and you can step through or understand the error messages as any other source file.
> 
> In the meantime, have you tried looking at the pre-processed file generated from that header? Perhaps you could keep it expanded until you move to table-gen?
Certainly, the code from my previous comment is taken from the preprocessed file. I'll put the expanded macros to the BaseInfo.



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116464/new/

https://reviews.llvm.org/D116464



More information about the llvm-commits mailing list