Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] Add support for global ctors / dtors for AMDGPU
This patch makes the necessary changes to support calling global constructors and destructors on the GPU. The patch in D149340 allows the `lld` linker to create the symbols pointing us to these globals. These should be executed by a single thread, which is more difficult on the GPU because all threads are active. I chose to use an atomic counter to sync every thread on the GPU. This is very slow if you use more than a few thousand threads, but for testing purposes it should be sufficient. Depends on D149340 D149363 Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D149398
- Loading branch information
Showing
4 changed files
with
135 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
libc/test/integration/startup/gpu/init_fini_array_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//===-- Loader test to test init and fini array iteration -----------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "test/IntegrationTest/test.h" | ||
|
||
#include <stddef.h> | ||
|
||
int global_destroyed = false; | ||
|
||
class A { | ||
private: | ||
int val[1024]; | ||
|
||
public: | ||
A(int i, int a) { | ||
for (int k = 0; k < 1024; ++k) | ||
val[k] = 0; | ||
val[i] = a; | ||
} | ||
|
||
~A() { global_destroyed = true; } | ||
|
||
int get(int i) const { return val[i]; } | ||
}; | ||
|
||
int GLOBAL_INDEX = 512; | ||
int INITVAL_INITIALIZER = 0x600D; | ||
int BEFORE_INITIALIZER = 0xFEED; | ||
|
||
A global(GLOBAL_INDEX, INITVAL_INITIALIZER); | ||
|
||
int initval = 0; | ||
int before = 0; | ||
|
||
__attribute__((constructor(101))) void run_before() { | ||
before = BEFORE_INITIALIZER; | ||
} | ||
|
||
__attribute__((constructor(65535))) void run_after() { | ||
ASSERT_EQ(before, BEFORE_INITIALIZER); | ||
} | ||
|
||
__attribute__((constructor)) void set_initval() { | ||
initval = INITVAL_INITIALIZER; | ||
} | ||
__attribute__((destructor(1))) void reset_initval() { | ||
ASSERT_TRUE(global_destroyed); | ||
initval = 0; | ||
} | ||
|
||
TEST_MAIN() { | ||
ASSERT_EQ(global.get(GLOBAL_INDEX), INITVAL_INITIALIZER); | ||
ASSERT_EQ(initval, INITVAL_INITIALIZER); | ||
return 0; | ||
} |