Skip to content

Commit

Permalink
Addressed reviewer comments
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fabianmcg committed Jan 18, 2024
1 parent b2f2563 commit b385148
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 19 deletions.
8 changes: 5 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "CIRGenBuilder.h"
#include "CIRGenCstEmitter.h"
#include "CIRGenFunction.h"
#include "CIRGenOpenMPRuntime.h"
#include "EHScopeStack.h"
#include "UnimplementedFeatureGuarding.h"
#include "mlir/IR/Attributes.h"
Expand Down Expand Up @@ -54,9 +55,10 @@ CIRGenFunction::buildAutoVarAlloca(const VarDecl &D) {

Address address = Address::invalid();
Address allocaAddr = Address::invalid();
// TODO[OpenMP]: Set openMPLocalAddr with the equivalent of
// getOpenMPRuntime().getAddressOfLocalVariable().
Address openMPLocalAddr = Address::invalid();
Address openMPLocalAddr =
getCIRGenModule().getOpenMPRuntime().getAddressOfLocalVariable(*this, &D);
if (getLangOpts().OpenMPIsTargetDevice)
assert(UnimplementedFeature::openMPTarget());
if (getLangOpts().OpenMP && openMPLocalAddr.isValid()) {
llvm_unreachable("NYI");
} else if (Ty->isConstantSizeType()) {
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "CIRGenCstEmitter.h"
#include "CIRGenFunction.h"
#include "CIRGenModule.h"
#include "CIRGenOpenMPRuntime.h"
#include "CIRGenValue.h"
#include "UnimplementedFeatureGuarding.h"

Expand Down Expand Up @@ -912,9 +913,9 @@ LValue CIRGenFunction::buildBinaryOperatorLValue(const BinaryOperator *E) {
} else {
buildStoreThroughLValue(RV, LV);
}

// TODO[OpenMP]: Check and handle assignment to a variable declared as
// last-private.
if (getLangOpts().OpenMP)
CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(*this,
E->getLHS());
return LV;
}

Expand Down
6 changes: 4 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "CIRDataLayout.h"
#include "CIRGenFunction.h"
#include "CIRGenModule.h"
#include "CIRGenOpenMPRuntime.h"
#include "UnimplementedFeatureGuarding.h"

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

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

Expand Down
4 changes: 3 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "CIRGenFunction.h"
#include "CIRGenCXXABI.h"
#include "CIRGenModule.h"
#include "CIRGenOpenMPRuntime.h"
#include "UnimplementedFeatureGuarding.h"

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

// TODO: prologuecleanupdepth

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

// TODO: buildFunctionProlog

Expand Down
19 changes: 11 additions & 8 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "CIRGenCXXABI.h"
#include "CIRGenCstEmitter.h"
#include "CIRGenFunction.h"
#include "CIRGenOpenMPRuntime.h"
#include "CIRGenTypes.h"
#include "CIRGenValue.h"
#include "TargetInfo.h"
Expand Down Expand Up @@ -103,7 +104,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
codeGenOpts(CGO),
theModule{mlir::ModuleOp::create(builder.getUnknownLoc())}, Diags(Diags),
target(astCtx.getTargetInfo()), ABI(createCXXABI(*this)), genTypes{*this},
VTables{*this} {
VTables{*this}, openMPRuntime(new CIRGenOpenMPRuntime(*this)) {

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

if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd && D)
assert(0 && "not implemented");
if (langOpts.OpenMP && !langOpts.OpenMPSimd && D)
getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);

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

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

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

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

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

void CIRGenModule::buildDeferred(unsigned recursionLimit) {
// Emit deferred declare target declarations
// TODO[OpenMP]: Emit deferred target declarations.
if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
getOpenMPRuntime().emitDeferredTargetDecls();

// Emit code for any potentially referenced deferred decls. Since a previously
// unused static decl may become used during the generation of code for a
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace cir {
class CIRGenFunction;
class CIRGenCXXABI;
class TargetCIRGenInfo;
class CIRGenOpenMPRuntime;

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

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

/// Holds the OpenMP runtime
std::unique_ptr<CIRGenOpenMPRuntime> openMPRuntime;

/// Per-function codegen information. Updated everytime buildCIR is called
/// for FunctionDecls's.
CIRGenFunction *CurCGF = nullptr;
Expand Down Expand Up @@ -626,6 +630,12 @@ class CIRGenModule : public CIRGenTypeCache {
/// Print out an error that codegen doesn't support the specified decl yet.
void ErrorUnsupported(const Decl *D, const char *Type);

/// Return a reference to the configured OpenMP runtime.
CIRGenOpenMPRuntime &getOpenMPRuntime() {
assert(openMPRuntime != nullptr);
return *openMPRuntime;
}

private:
// An ordered map of canonical GlobalDecls to their mangled names.
llvm::MapVector<clang::GlobalDecl, llvm::StringRef> MangledDeclNames;
Expand Down
54 changes: 54 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//===--- CIRGenStmtOpenMP.cpp - Interface to OpenMP 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 a class for OpenMP runtime MLIR code generation.
//
//===----------------------------------------------------------------------===//

#include "CIRGenOpenMPRuntime.h"
#include "CIRGenFunction.h"
#include "CIRGenModule.h"

using namespace cir;
using namespace clang;

CIRGenOpenMPRuntime::CIRGenOpenMPRuntime(CIRGenModule &CGM) : CGM(CGM) {}

Address CIRGenOpenMPRuntime::getAddressOfLocalVariable(CIRGenFunction &CGF,
const VarDecl *VD) {
// TODO[OpenMP]: Implement this method.
return Address::invalid();
}

void CIRGenOpenMPRuntime::checkAndEmitLastprivateConditional(
CIRGenFunction &CGF, const Expr *LHS) {
// TODO[OpenMP]: Implement this method.
return;
}

void CIRGenOpenMPRuntime::registerTargetGlobalVariable(
const clang::VarDecl *VD, mlir::cir::GlobalOp globalOp) {
// TODO[OpenMP]: Implement this method.
return;
}

void CIRGenOpenMPRuntime::emitDeferredTargetDecls() const {
// TODO[OpenMP]: Implement this method.
return;
}

void CIRGenOpenMPRuntime::emitFunctionProlog(CIRGenFunction &CGF,
const clang::Decl *D) {
// TODO[OpenMP]: Implement this method.
return;
}

bool CIRGenOpenMPRuntime::emitTargetGlobal(clang::GlobalDecl &GD) {
// TODO[OpenMP]: Implement this method.
return false;
}
77 changes: 77 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//===--- CIRGenOpenMPRuntime.h - Interface to OpenMP 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 a class for OpenMP runtime MLIR code generation.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENOPENMPRUNTIME_H
#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENOPENMPRUNTIME_H

#include "CIRGenValue.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"

namespace clang {
class Decl;
class Expr;
class GlobalDecl;
class VarDecl;
} // namespace clang

namespace cir {
class CIRGenModule;
class CIRGenFunction;

class CIRGenOpenMPRuntime {
public:
explicit CIRGenOpenMPRuntime(CIRGenModule &CGM);
virtual ~CIRGenOpenMPRuntime() {}

/// Gets the OpenMP-specific address of the local variable.
virtual Address getAddressOfLocalVariable(CIRGenFunction &CGF,
const clang::VarDecl *VD);

/// Checks if the provided \p LVal is lastprivate conditional and emits the
/// code to update the value of the original variable.
/// \code
/// lastprivate(conditional: a)
/// ...
/// <type> a;
/// lp_a = ...;
/// #pragma omp critical(a)
/// if (last_iv_a <= iv) {
/// last_iv_a = iv;
/// global_a = lp_a;
/// }
/// \endcode
virtual void checkAndEmitLastprivateConditional(CIRGenFunction &CGF,
const clang::Expr *LHS);

/// Checks if the provided global decl \a GD is a declare target variable and
/// registers it when emitting code for the host.
virtual void registerTargetGlobalVariable(const clang::VarDecl *VD,
mlir::cir::GlobalOp globalOp);

/// Emit deferred declare target variables marked for deferred emission.
void emitDeferredTargetDecls() const;

/// Emits OpenMP-specific function prolog.
/// Required for device constructs.
virtual void emitFunctionProlog(CIRGenFunction &CGF, const clang::Decl *D);

/// Emit the global \a GD if it is meaningful for the target. Returns
/// if it was emitted successfully.
/// \param GD Global to scan.
virtual bool emitTargetGlobal(clang::GlobalDecl &D);

protected:
CIRGenModule &CGM;
};
} // namespace cir

#endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENOPENMPRUNTIME_H
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
//===----------------------------------------------------------------------===//

#include "CIRGenFunction.h"
#include "CIRGenOpenMPRuntime.h"
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"

using namespace cir;
using namespace clang;
using namespace mlir::cir;
using namespace mlir::omp;

mlir::LogicalResult
Expand All @@ -28,7 +28,7 @@ CIRGenFunction::buildOMPParallelDirective(const OMPParallelDirective &S) {
mlir::OpBuilder::InsertionGuard guardCase(builder);
builder.setInsertionPointToEnd(&block);
// Create a scope for the OpenMP region.
builder.create<ScopeOp>(
builder.create<mlir::cir::ScopeOp>(
scopeLoc, /*scopeBuilder=*/
[&](mlir::OpBuilder &b, mlir::Location loc) {
LexicalScope lexScope{*this, scopeLoc, builder.getInsertionBlock()};
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_clang_library(clangCIR
CIRGenFunction.cpp
CIRGenItaniumCXXABI.cpp
CIRGenModule.cpp
CIRGenOpenMPRuntime.cpp
CIRGenStmt.cpp
CIRGenStmtOpenMP.cpp
CIRGenTBAA.cpp
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ struct UnimplementedFeature {
static bool cxxABI() { return false; }
static bool openCL() { return false; }
static bool openMP() { return false; }
static bool openMPRuntime() { return false; }
static bool openMPTarget() { return false; }
static bool ehStack() { return false; }
static bool isVarArg() { return false; }
static bool setNonGC() { return false; }
Expand Down

0 comments on commit b385148

Please sign in to comment.