forked from llvm/clangir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CIR][CodeGen] Set address space for OpenCL static and local-qualifie…
…d variables (llvm#792) In OpenCL, `local`-qualified variables are implicitly considered as static. In order to support it, this PR unblocks code paths related to OpenCL static declarations in `CIRGenDecl.cpp`. Following the approach of LLVM CodeGen, a new class `CIRGenOpenCLRuntime` is added to handle the language hook of creating `local`-qualified variables. The default behavior of this hook is quite simple. It forwards the call to `CGF.buildStaticVarDecl`. So in CIR, the OpenCL local memory representation is equivalent to the one defined by SPIR-LLVM convention: a `cir.global` with `addrspace(local)` and *without initializer*, which corresponds to LLVM `undef` initializer. See check lines in test for more details. A `static global`-qualified variable is also added in the test to exercise the static code path itself.
- Loading branch information
1 parent
128e7c0
commit 624beb6
Showing
7 changed files
with
127 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
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,29 @@ | ||
//===-- CIRGenOpenCLRuntime.cpp - Interface to OpenCL Runtimes ------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This provides an abstract class for OpenCL CIR generation. Concrete | ||
// subclasses of this implement code generation for specific OpenCL | ||
// runtime libraries. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "CIRGenOpenCLRuntime.h" | ||
#include "CIRGenFunction.h" | ||
|
||
#include "clang/CIR/Dialect/IR/CIROpsEnums.h" | ||
|
||
using namespace clang; | ||
using namespace cir; | ||
|
||
CIRGenOpenCLRuntime::~CIRGenOpenCLRuntime() {} | ||
|
||
void CIRGenOpenCLRuntime::buildWorkGroupLocalVarDecl(CIRGenFunction &CGF, | ||
const VarDecl &D) { | ||
return CGF.buildStaticVarDecl(D, | ||
mlir::cir::GlobalLinkageKind::InternalLinkage); | ||
} |
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,46 @@ | ||
//===-- CIRGenOpenCLRuntime.h - Interface to OpenCL Runtimes -----*- C++ -*-==// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This provides an abstract class for OpenCL CIR generation. Concrete | ||
// subclasses of this implement code generation for specific OpenCL | ||
// runtime libraries. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_LIB_CIR_CIRGENOPENCLRUNTIME_H | ||
#define LLVM_CLANG_LIB_CIR_CIRGENOPENCLRUNTIME_H | ||
|
||
namespace clang { | ||
|
||
class VarDecl; | ||
|
||
} // namespace clang | ||
|
||
namespace cir { | ||
|
||
class CIRGenFunction; | ||
class CIRGenModule; | ||
|
||
class CIRGenOpenCLRuntime { | ||
protected: | ||
CIRGenModule &CGM; | ||
|
||
public: | ||
CIRGenOpenCLRuntime(CIRGenModule &CGM) : CGM(CGM) {} | ||
virtual ~CIRGenOpenCLRuntime(); | ||
|
||
/// Emit the IR required for a work-group-local variable declaration, and add | ||
/// an entry to CGF's LocalDeclMap for D. The base class does this using | ||
/// CIRGenFunction::EmitStaticVarDecl to emit an internal global for D. | ||
virtual void buildWorkGroupLocalVarDecl(CIRGenFunction &CGF, | ||
const clang::VarDecl &D); | ||
}; | ||
|
||
} // namespace cir | ||
|
||
#endif // LLVM_CLANG_LIB_CIR_CIRGENOPENCLRUNTIME_H |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// RUN: %clang_cc1 -cl-std=CL3.0 -O0 -fclangir -emit-cir -triple spirv64-unknown-unknown %s -o %t.cir | ||
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR | ||
// RUN: %clang_cc1 -cl-std=CL3.0 -O0 -fclangir -emit-llvm -triple spirv64-unknown-unknown %s -o %t.ll | ||
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM | ||
|
||
kernel void test_static(int i) { | ||
static global int b = 15; | ||
// CIR-DAG: cir.global "private" internal dsolocal addrspace(offload_global) @test_static.b = #cir.int<15> : !s32i {alignment = 4 : i64} | ||
// LLVM-DAG: @test_static.b = internal addrspace(1) global i32 15 | ||
|
||
local int c; | ||
// CIR-DAG: cir.global "private" internal dsolocal addrspace(offload_local) @test_static.c : !s32i {alignment = 4 : i64} | ||
// LLVM-DAG: @test_static.c = internal addrspace(3) global i32 undef | ||
|
||
// CIR-DAG: %[[#ADDRB:]] = cir.get_global @test_static.b : !cir.ptr<!s32i, addrspace(offload_global)> | ||
// CIR-DAG: %[[#ADDRC:]] = cir.get_global @test_static.c : !cir.ptr<!s32i, addrspace(offload_local)> | ||
|
||
c = b; | ||
// CIR: %[[#LOADB:]] = cir.load %[[#ADDRB]] : !cir.ptr<!s32i, addrspace(offload_global)>, !s32i | ||
// CIR-NEXT: cir.store %[[#LOADB]], %[[#ADDRC]] : !s32i, !cir.ptr<!s32i, addrspace(offload_local)> | ||
|
||
// LLVM: %[[#LOADB:]] = load i32, ptr addrspace(1) @test_static.b, align 4 | ||
// LLVM-NEXT: store i32 %[[#LOADB]], ptr addrspace(3) @test_static.c, align 4 | ||
} |