Constexpr in CUDA


I have a questions on how CUDA implements ‘constexpr’. My understanding was that a constexpr is a kind of ’
typed’ define. A constant that have a type and a value. So it should not use any memory at all since it is just a constant. I use code like this in my kernel:

constexpr const uint32_t ce_ui32CacheItemBytes = 64;

and I’m surprised that I found this in the ptx file:

.global .align 4 .u32 ce_ui32CacheItemBytes = 64;

So this constant is - other than expected - stored in global memory. Why?
Maybe I miss a keyword or have any other issue that avoids the compiler from removing this const? Can anyone please bring some light into this?


Are you looking at the PTX from a debug build or a release build? In the generated PTX code, do you see ce_ui32CacheItemBytes loaded from memory anywhere?

Can you post a minimal complete (buildable, runnable, no external dependencies) reproducer code?

Hi. This is a release build and the var is never touched in the ptx, so it is not used, but it sits in the global memory.

never touched in the ptx, so it is not used

That is what I would expect for a release build.

but it sits in the global memory

How is this a problem? I don’t see anything in the C++ standard that says that constexpr data cannot be stored(but I may have missed it in the volumnious document). My understanding is that the difference between const and constexpr is mostly one between potential run time initialization versus guaranteed compile time initialization.

Why are you using constexpr const here? My understanding is that these are alternatives between which programmers can choose: either const or constexpr. Did I miss something?

I was asking just for my understanding. I use conexpr for typesafty. I have had defines before and changed them to constexpr. I couldn’t understand why I see them in global memory - not saying that this is a problem. I just wanted to understand why they are there - if they are not used. With defined I do not see them, but my code is not as clean.