[llvm] Reapply "[LLVM][TableGen] Parameterize NumToSkip in DecoderEmitter" (#136017) (PR #136019)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 17 18:00:59 PDT 2025


jurahul wrote:

Ok, I think I managed to create a small repro. It's not the `decodeInstructions` in my earlier commit, it's the `decodeNumToSkip` which is marked inline (but not static). 

```
inline unsigned decodeNumToSkip(const uint8_t *&Ptr) {
  unsigned NumToSkip = *Ptr++;
  NumToSkip |= (*Ptr++) << 8;
  if constexpr (NumToSkipSizeInBytes == 3)
    NumToSkip |= (*Ptr++) << 16;
  return NumToSkip;
}
``` 

So different CUs will get different versions of this function and since its not marked static, it can be merged at link time. A simple C++ example shows that this can happen as follows:

```
 cat *.cpp
// file1.cpp
#include <iostream>
using namespace std;

namespace llvm {
inline int getNumToSkip() { return 1; }
}


void print1() {
   cout << llvm::getNumToSkip() << '\n';
}

// file2.cpp
#include <iostream>
using namespace std;

namespace llvm {
inline int getNumToSkip() { return 2; }
}

void print2() {
   cout << llvm::getNumToSkip() << '\n';
}

// main.cpp
extern void print1();
extern void print2();

int main() {
        print1();
        print2();
}

$ g++ file1.cpp file2.cpp main.cpp -O3 && ./a.out
1
2
$ g++ file1.cpp file2.cpp main.cpp -O0 && ./a.out
1
1
$ clang++ file1.cpp file2.cpp main.cpp -O3 && ./a.out
1
2
$ clang++ file1.cpp file2.cpp main.cpp -O0 && ./a.out
1
1
```

so just making it static inline should fix it as well, as is proved below:

```
$ cat *.cpp
// file1.cpp
#include <iostream>
using namespace std;

namespace llvm {
static inline int getNumToSkip() { return 1; }
}


void print1() {
   cout << llvm::getNumToSkip() << '\n';
}

// file2.cpp
#include <iostream>
using namespace std;

namespace llvm {
static inline int getNumToSkip() { return 2; }
}

void print2() {
   cout << llvm::getNumToSkip() << '\n';
}

// main.cpp
extern void print1();
extern void print2();

int main() {
        print1();
        print2();
}

$ g++ file1.cpp file2.cpp main.cpp -O3 && ./a.out
1
2
$ g++ file1.cpp file2.cpp main.cpp -O0 && ./a.out
1
2
$ clang++ file1.cpp file2.cpp main.cpp -O3 && ./a.out
1
2
$ clang++ file1.cpp file2.cpp main.cpp -O0 && ./a.out
1
2
```

https://github.com/llvm/llvm-project/pull/136019


More information about the llvm-commits mailing list