clangupc / upc2c

Clang based UPC to C Translator
https://clangupc.github.io/clang-upc2c
Other
4 stars 3 forks source link

TLD: invalid initializers for file scope variables #103

Closed PHHargrove closed 10 years ago

PHHargrove commented 10 years ago

The following error on bug544 indicates that the definition _upcr_pthreadinfo is missing, but that is just a symptom rather than the true problem:

[bugzilla/bug544_st04] FAILED: UPC-To-C Translation or Link error       (NEW)
cd /home/phargrov/upc-runtime/BUILD-gcc64/dbg_cupc2c/upc-tests/bugzilla
/home/phargrov/upc-runtime/BUILD-gcc64/dbg_cupc2c/upcc -Ww,-Wno-duplicate-decl-specifier -Ww,-Werror=pointer-arith -g  -network=smp -pthreads -nolink-cache -T 4 -o bug544_st04 bug544.upc
upcc: Error building pthread objects with thread-local data:
bug544_obj.c:15:57: error: use of undeclared identifier '_upcr_pthreadinfo'; did you mean 'upcri_pthreadinfo_t'?
int * UPCR_TLD_DEFINE(pfoo, 8, 8) = &((*(_cupc2c_tld0 *)UPCR_TLD_ADDR(foo))[2]);
                                                        ^
/home/phargrov/upc-runtime/upcr_threads.h:348:64: note: expanded from macro 'UPCR_TLD_ADDR'
#  define UPCR_TLD_ADDR(name) ((void *)&((struct upcri_tld_t *)upcri_mypthreadinfo())->name)
                                                               ^
/home/phargrov/upc-runtime/upcr_threads.h:289:33: note: expanded from macro 'upcri_mypthreadinfo'
  #define upcri_mypthreadinfo() _upcr_pthreadinfo
                                ^
/home/phargrov/upc-runtime/upcr_threads.h:280:3: note: 'upcri_pthreadinfo_t' declared here
} upcri_pthreadinfo_t;
  ^

The clang-upc2c translation of bug544 contains:

typedef int _cupc2c_tld0[100];
_cupc2c_tld0 UPCR_TLD_DEFINE(foo, 400, 4);
int * UPCR_TLD_DEFINE(pfoo, 8, 8) = &((*(_cupc2c_tld0 *)UPCR_TLD_ADDR(foo))[2]);
int * UPCR_TLD_DEFINE(pcrazy, 8, 8) = &((*(_cupc2c_tld0 *)UPCR_TLD_ADDR(foo))[(&((*(_cupc2c_tld0 *)UPCR_TLD_ADDR(foo))[40]) - &((*(_cupc2c_tld0 *)UPCR_TLD_ADDR(foo))[30]))]);

The problem lies in the initializers for pfoo and pcrazy, which are themselves thread-specific values (expressions containing addresses of TLD). This produces an error because UPCR_TLD_ADDR() appears in the initializer and accesses the thread-specific _upcr_pthreadinfo variable, which is instantiated by UPCR_BEGIN_FUNCTION().

Meanwhile the Berkeley translator produces an error message (or two) instead:

upcc: error during UPC-to-C translation (sgiupc stage): 
bug544.upc:7: Initializer expression for 'pfoo' not supported by BUPC
bug544.upc:8: Initializer expression for 'pcrazy' not supported by BUPC
bug544.upc:8: Initializer expression for 'pcrazy' not supported by BUPC
PHHargrove commented 10 years ago

The bug7 test is an example of a thread-specific TLD initializer that Berkeley UPC can handle, but clang-upc2c currently gets wrong.

The source contains:

extern int quux;
int * pquux = &quux;
[....]
int quux;

For which the Berkeley UPC translator generates code with an initializer appropriate only to the initial pthread (though no initializer is actually required at all):

extern int quux;
int*  UPCR_TLD_DEFINE(pquux, 8, 8) = &quux;
int UPCR_TLD_DEFINE_TENTATIVE(quux, 4, 4);

PLUS the initialzier code which sets the right value for any thread:

void UPCRI_INIT_bug7_6953366925489(void) {
UPCR_BEGIN_FUNCTION();
UPCR_SET_SRCPOS("_bug7_6953366925489_INIT",0);
*((int* *) UPCR_TLD_ADDR(pquux)) = (int* )UPCR_TLD_ADDR(quux);
}

The translation from clang-upc2c attempts to perform the thread-specific initialization at file scope, and no initialization code:

int UPCR_TLD_DEFINE(quux, 4, 4);
int * UPCR_TLD_DEFINE(pquux, 8, 8) = &(*(int *)UPCR_TLD_ADDR(quux));
[...]
int UPCR_TLD_DEFINE(quux, 4, 4);

Also note that the "extern int quux" has been translated to a NON-tentative definition, which is probably not correct in general.

PHHargrove commented 10 years ago

This appears to be resolved by 4a3b381. Since this issue is a regression relative to master (created and resolved on the branch) I am closing it.

PHHargrove commented 10 years ago

The dynamic initializers generated for pfoo and pcrazy in order to fix this issue are incorrect. So, I am reopenning this issue rather than creating a new one.

The current problem is that the TLD accessor macro does not appear on the LHS of the following:

    pfoo = &((*(_cupc2c_tld0 *)UPCR_TLD_ADDR(foo))[2]);
    pcrazy = &((*(_cupc2c_tld0 *)UPCR_TLD_ADDR(foo))[(&((*(_cupc2c_tld0 *)UPCR_TLD_ADDR(foo))[40]) - &((*(_cupc2c_tld0 *)UPCR_TLD_ADDR(foo))[30]))]);

The left-hand sides should also be expressions involving UPCR_TLD_ADDR().

PHHargrove commented 10 years ago

A possible fix is now in testing.

PHHargrove commented 10 years ago

Fixed in 59ca342