Sema : Section type conflict

Hi,

The change at ⚙ D102693 Do not create LLVM IR `constant`s for objects with dynamic initialisation causes conflicting type if a const is having _attribute _((section inside a template which is not instantiated. The const is defined though. With the change, the section is getting marked with BSS segment with RW instead of RO causing conflict.

The piece of code causing this conflict is in lib/Sema/SemaDecl.cpp function Sema::CheckCompleteVariableDeclaration()

  if (HasConstInit)
    Stack = &ConstSegStack;
  else {
    Stack = &BSSSegStack;
    SectionFlags |= ASTContext::PSF_Write;
  }

Any thoughts how to resolve this issue?

Hi @chill, tagging you since the patch ⚙ D102693 Do not create LLVM IR `constant`s for objects with dynamic initialisation is authored by you. Let me know your thoughts.

Hello,

I’m afraid I couldn’t create a reproducer out of the textual description.
I would suggest you create a minimal testcase and file a bug report.

@chill sharing test case:

typedef signed long int int32;
typedef unsigned long int uint32;

typedef struct
{
  uint32 var;
  const char *ptr;
}
basestruct;


typedef struct
{
  basestruct msg_const_string;
}
test_struct_type;


template<class KeyType>
class AvlNode {
public:
  int32 test1() const;
};


template<class KeyType>
int32
AvlNode<KeyType>::test1() const
{
    static const char string_ptr[] __attribute__((section ("testptr.rodata."))) = {"test1"};
	static const test_struct_type test_struct __attribute__((section ("test.rodata." )))= { { (0x00000002), (char*)&string_ptr} };
}

__attribute__((noinline))
void test2()
{
	static const char string_ptr[] __attribute__((section ("testptr.rodata."))) = {"test2"}; 
	static const test_struct_type test_struct __attribute__((section ("test.rodata."))) = { { (0x00000002), (char*)&string_ptr} };
}

I get below error :

error: 'test_struct' causes a section type conflict with 'test_struct'
        static const test_struct_type test_struct __attribute__((section ("test.rodata."))) = { { (0x00000002), (char*)&string_ptr} };
                                      ^
section_conflict_test.ii:31:32: note: declared here
        static const test_struct_type test_struct __attribute__((section ("test.rodata." )))= { { (0x00000002), (char*)&string_ptr} };
                                      ^
1 error generated.

Looks like it’s the same issue as in Regression since "Do not create LLVM IR `constant`s for objects with dynamic initialisation" · Issue #50784 · llvm/llvm-project · GitHub

I hope this example is sufficiently close to the original programs, as the simple solution is just to remove the cast.