Skip to content

Commit b385148

Browse files
committed
Addressed reviewer comments
I left the `TODO[OpenMP]:` comments in `CIRGenExpr.cpp` as it is necessary to remove assertions to test and ithat specific issue needs to be addressed quickly in a different patch on captured variables.
1 parent b2f2563 commit b385148

11 files changed

+173
-19
lines changed

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CIRGenBuilder.h"
1515
#include "CIRGenCstEmitter.h"
1616
#include "CIRGenFunction.h"
17+
#include "CIRGenOpenMPRuntime.h"
1718
#include "EHScopeStack.h"
1819
#include "UnimplementedFeatureGuarding.h"
1920
#include "mlir/IR/Attributes.h"
@@ -54,9 +55,10 @@ CIRGenFunction::buildAutoVarAlloca(const VarDecl &D) {
5455

5556
Address address = Address::invalid();
5657
Address allocaAddr = Address::invalid();
57-
// TODO[OpenMP]: Set openMPLocalAddr with the equivalent of
58-
// getOpenMPRuntime().getAddressOfLocalVariable().
59-
Address openMPLocalAddr = Address::invalid();
58+
Address openMPLocalAddr =
59+
getCIRGenModule().getOpenMPRuntime().getAddressOfLocalVariable(*this, &D);
60+
if (getLangOpts().OpenMPIsTargetDevice)
61+
assert(UnimplementedFeature::openMPTarget());
6062
if (getLangOpts().OpenMP && openMPLocalAddr.isValid()) {
6163
llvm_unreachable("NYI");
6264
} else if (Ty->isConstantSizeType()) {

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "CIRGenCstEmitter.h"
1616
#include "CIRGenFunction.h"
1717
#include "CIRGenModule.h"
18+
#include "CIRGenOpenMPRuntime.h"
1819
#include "CIRGenValue.h"
1920
#include "UnimplementedFeatureGuarding.h"
2021

@@ -912,9 +913,9 @@ LValue CIRGenFunction::buildBinaryOperatorLValue(const BinaryOperator *E) {
912913
} else {
913914
buildStoreThroughLValue(RV, LV);
914915
}
915-
916-
// TODO[OpenMP]: Check and handle assignment to a variable declared as
917-
// last-private.
916+
if (getLangOpts().OpenMP)
917+
CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(*this,
918+
E->getLHS());
918919
return LV;
919920
}
920921

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CIRDataLayout.h"
1515
#include "CIRGenFunction.h"
1616
#include "CIRGenModule.h"
17+
#include "CIRGenOpenMPRuntime.h"
1718
#include "UnimplementedFeatureGuarding.h"
1819

1920
#include "clang/AST/StmtVisitor.h"
@@ -1805,8 +1806,9 @@ LValue ScalarExprEmitter::buildCompoundAssignLValue(
18051806
else
18061807
CGF.buildStoreThroughLValue(RValue::get(Result), LHSLV);
18071808

1808-
// TODO[OpenMP]: Check and handle assignment to a variable declared as
1809-
// last-private.
1809+
if (CGF.getLangOpts().OpenMP)
1810+
CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF,
1811+
E->getLHS());
18101812
return LHSLV;
18111813
}
18121814

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "CIRGenFunction.h"
1414
#include "CIRGenCXXABI.h"
1515
#include "CIRGenModule.h"
16+
#include "CIRGenOpenMPRuntime.h"
1617
#include "UnimplementedFeatureGuarding.h"
1718

1819
#include "clang/AST/ASTLambda.h"
@@ -973,7 +974,8 @@ void CIRGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
973974

974975
// TODO: prologuecleanupdepth
975976

976-
// TODO[OpenMP]: Emit OpenMP specific initialization of the device functions.
977+
if (getLangOpts().OpenMP && CurCodeDecl)
978+
CGM.getOpenMPRuntime().emitFunctionProlog(*this, CurCodeDecl);
977979

978980
// TODO: buildFunctionProlog
979981

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "CIRGenCXXABI.h"
1616
#include "CIRGenCstEmitter.h"
1717
#include "CIRGenFunction.h"
18+
#include "CIRGenOpenMPRuntime.h"
1819
#include "CIRGenTypes.h"
1920
#include "CIRGenValue.h"
2021
#include "TargetInfo.h"
@@ -103,7 +104,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
103104
codeGenOpts(CGO),
104105
theModule{mlir::ModuleOp::create(builder.getUnknownLoc())}, Diags(Diags),
105106
target(astCtx.getTargetInfo()), ABI(createCXXABI(*this)), genTypes{*this},
106-
VTables{*this} {
107+
VTables{*this}, openMPRuntime(new CIRGenOpenMPRuntime(*this)) {
107108

108109
// Initialize CIR signed integer types cache.
109110
SInt8Ty =
@@ -613,8 +614,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
613614
!D->hasAttr<clang::DLLExportAttr>())
614615
assert(!UnimplementedFeature::setDLLStorageClass() && "NYI");
615616

616-
if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd && D)
617-
assert(0 && "not implemented");
617+
if (langOpts.OpenMP && !langOpts.OpenMPSimd && D)
618+
getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);
618619

619620
// TODO(cir): check TargetAS matches Entry address space
620621
if (Entry.getSymType() == Ty &&
@@ -685,7 +686,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
685686

686687
// Handle things which are present even on external declarations.
687688
if (D) {
688-
// TODO[OpenMP]: Check and handle target globals.
689+
if (langOpts.OpenMP && !langOpts.OpenMPSimd && D)
690+
getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);
689691

690692
// FIXME: This code is overly simple and should be merged with other global
691693
// handling.
@@ -2299,17 +2301,18 @@ void CIRGenModule::buildGlobalDecl(clang::GlobalDecl &D) {
22992301
}
23002302

23012303
// If this is OpenMP, check if it is legal to emit this global normally.
2302-
if (getLangOpts().OpenMP) {
2303-
// TODO[OpenMP]: Emit target globals.
2304-
}
2304+
if (getLangOpts().OpenMP && openMPRuntime &&
2305+
openMPRuntime->emitTargetGlobal(D))
2306+
return;
23052307

23062308
// Otherwise, emit the definition and move on to the next one.
23072309
buildGlobalDefinition(D, Op);
23082310
}
23092311

23102312
void CIRGenModule::buildDeferred(unsigned recursionLimit) {
23112313
// Emit deferred declare target declarations
2312-
// TODO[OpenMP]: Emit deferred target declarations.
2314+
if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
2315+
getOpenMPRuntime().emitDeferredTargetDecls();
23132316

23142317
// Emit code for any potentially referenced deferred decls. Since a previously
23152318
// unused static decl may become used during the generation of code for a

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace cir {
4646
class CIRGenFunction;
4747
class CIRGenCXXABI;
4848
class TargetCIRGenInfo;
49+
class CIRGenOpenMPRuntime;
4950

5051
enum ForDefinition_t : bool { NotForDefinition = false, ForDefinition = true };
5152

@@ -100,6 +101,9 @@ class CIRGenModule : public CIRGenTypeCache {
100101
/// Holds information about C++ vtables.
101102
CIRGenVTables VTables;
102103

104+
/// Holds the OpenMP runtime
105+
std::unique_ptr<CIRGenOpenMPRuntime> openMPRuntime;
106+
103107
/// Per-function codegen information. Updated everytime buildCIR is called
104108
/// for FunctionDecls's.
105109
CIRGenFunction *CurCGF = nullptr;
@@ -626,6 +630,12 @@ class CIRGenModule : public CIRGenTypeCache {
626630
/// Print out an error that codegen doesn't support the specified decl yet.
627631
void ErrorUnsupported(const Decl *D, const char *Type);
628632

633+
/// Return a reference to the configured OpenMP runtime.
634+
CIRGenOpenMPRuntime &getOpenMPRuntime() {
635+
assert(openMPRuntime != nullptr);
636+
return *openMPRuntime;
637+
}
638+
629639
private:
630640
// An ordered map of canonical GlobalDecls to their mangled names.
631641
llvm::MapVector<clang::GlobalDecl, llvm::StringRef> MangledDeclNames;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===--- CIRGenStmtOpenMP.cpp - Interface to OpenMP Runtimes --------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This provides a class for OpenMP runtime MLIR code generation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "CIRGenOpenMPRuntime.h"
14+
#include "CIRGenFunction.h"
15+
#include "CIRGenModule.h"
16+
17+
using namespace cir;
18+
using namespace clang;
19+
20+
CIRGenOpenMPRuntime::CIRGenOpenMPRuntime(CIRGenModule &CGM) : CGM(CGM) {}
21+
22+
Address CIRGenOpenMPRuntime::getAddressOfLocalVariable(CIRGenFunction &CGF,
23+
const VarDecl *VD) {
24+
// TODO[OpenMP]: Implement this method.
25+
return Address::invalid();
26+
}
27+
28+
void CIRGenOpenMPRuntime::checkAndEmitLastprivateConditional(
29+
CIRGenFunction &CGF, const Expr *LHS) {
30+
// TODO[OpenMP]: Implement this method.
31+
return;
32+
}
33+
34+
void CIRGenOpenMPRuntime::registerTargetGlobalVariable(
35+
const clang::VarDecl *VD, mlir::cir::GlobalOp globalOp) {
36+
// TODO[OpenMP]: Implement this method.
37+
return;
38+
}
39+
40+
void CIRGenOpenMPRuntime::emitDeferredTargetDecls() const {
41+
// TODO[OpenMP]: Implement this method.
42+
return;
43+
}
44+
45+
void CIRGenOpenMPRuntime::emitFunctionProlog(CIRGenFunction &CGF,
46+
const clang::Decl *D) {
47+
// TODO[OpenMP]: Implement this method.
48+
return;
49+
}
50+
51+
bool CIRGenOpenMPRuntime::emitTargetGlobal(clang::GlobalDecl &GD) {
52+
// TODO[OpenMP]: Implement this method.
53+
return false;
54+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===--- CIRGenOpenMPRuntime.h - Interface to OpenMP Runtimes -------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This provides a class for OpenMP runtime MLIR code generation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENOPENMPRUNTIME_H
14+
#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENOPENMPRUNTIME_H
15+
16+
#include "CIRGenValue.h"
17+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
18+
19+
namespace clang {
20+
class Decl;
21+
class Expr;
22+
class GlobalDecl;
23+
class VarDecl;
24+
} // namespace clang
25+
26+
namespace cir {
27+
class CIRGenModule;
28+
class CIRGenFunction;
29+
30+
class CIRGenOpenMPRuntime {
31+
public:
32+
explicit CIRGenOpenMPRuntime(CIRGenModule &CGM);
33+
virtual ~CIRGenOpenMPRuntime() {}
34+
35+
/// Gets the OpenMP-specific address of the local variable.
36+
virtual Address getAddressOfLocalVariable(CIRGenFunction &CGF,
37+
const clang::VarDecl *VD);
38+
39+
/// Checks if the provided \p LVal is lastprivate conditional and emits the
40+
/// code to update the value of the original variable.
41+
/// \code
42+
/// lastprivate(conditional: a)
43+
/// ...
44+
/// <type> a;
45+
/// lp_a = ...;
46+
/// #pragma omp critical(a)
47+
/// if (last_iv_a <= iv) {
48+
/// last_iv_a = iv;
49+
/// global_a = lp_a;
50+
/// }
51+
/// \endcode
52+
virtual void checkAndEmitLastprivateConditional(CIRGenFunction &CGF,
53+
const clang::Expr *LHS);
54+
55+
/// Checks if the provided global decl \a GD is a declare target variable and
56+
/// registers it when emitting code for the host.
57+
virtual void registerTargetGlobalVariable(const clang::VarDecl *VD,
58+
mlir::cir::GlobalOp globalOp);
59+
60+
/// Emit deferred declare target variables marked for deferred emission.
61+
void emitDeferredTargetDecls() const;
62+
63+
/// Emits OpenMP-specific function prolog.
64+
/// Required for device constructs.
65+
virtual void emitFunctionProlog(CIRGenFunction &CGF, const clang::Decl *D);
66+
67+
/// Emit the global \a GD if it is meaningful for the target. Returns
68+
/// if it was emitted successfully.
69+
/// \param GD Global to scan.
70+
virtual bool emitTargetGlobal(clang::GlobalDecl &D);
71+
72+
protected:
73+
CIRGenModule &CGM;
74+
};
75+
} // namespace cir
76+
77+
#endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENOPENMPRUNTIME_H

clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "CIRGenFunction.h"
14+
#include "CIRGenOpenMPRuntime.h"
1415
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
1516

1617
using namespace cir;
1718
using namespace clang;
18-
using namespace mlir::cir;
1919
using namespace mlir::omp;
2020

2121
mlir::LogicalResult
@@ -28,7 +28,7 @@ CIRGenFunction::buildOMPParallelDirective(const OMPParallelDirective &S) {
2828
mlir::OpBuilder::InsertionGuard guardCase(builder);
2929
builder.setInsertionPointToEnd(&block);
3030
// Create a scope for the OpenMP region.
31-
builder.create<ScopeOp>(
31+
builder.create<mlir::cir::ScopeOp>(
3232
scopeLoc, /*scopeBuilder=*/
3333
[&](mlir::OpBuilder &b, mlir::Location loc) {
3434
LexicalScope lexScope{*this, scopeLoc, builder.getInsertionBlock()};

clang/lib/CIR/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_clang_library(clangCIR
2626
CIRGenFunction.cpp
2727
CIRGenItaniumCXXABI.cpp
2828
CIRGenModule.cpp
29+
CIRGenOpenMPRuntime.cpp
2930
CIRGenStmt.cpp
3031
CIRGenStmtOpenMP.cpp
3132
CIRGenTBAA.cpp

0 commit comments

Comments
 (0)