Skip to content

Commit

Permalink
[CIR][CodeGen][NFC] Add TBAAAccessInfo stubbed out and many usages of…
Browse files Browse the repository at this point in the history
… it (llvm#859)
  • Loading branch information
lanza authored Sep 19, 2024
1 parent 01a99e6 commit 9a85975
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 197 deletions.
4 changes: 1 addition & 3 deletions clang/lib/CIR/CodeGen/CIRGenAtomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
#include "CIRGenOpenMPRuntime.h"
#include "TargetInfo.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/MissingFeatures.h"
#include "clang/CodeGen/CGFunctionInfo.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "llvm/Support/ErrorHandling.h"
#include <cstdint>
Expand Down Expand Up @@ -179,7 +177,7 @@ class AtomicInfo {
llvm_unreachable("NYI");

return LValue::makeAddr(addr, getValueType(), CGF.getContext(),
LVal.getBaseInfo());
LVal.getBaseInfo(), LVal.getTBAAInfo());
}

/// Emits atomic load.
Expand Down
259 changes: 136 additions & 123 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ CIRGenFunction::buildCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
// Emit the 'this' pointer.
Address This = Address::invalid();
if (BO->getOpcode() == BO_PtrMemI)
This = buildPointerWithAlignment(BaseExpr, nullptr, KnownNonNull);
This = buildPointerWithAlignment(BaseExpr, nullptr, nullptr, KnownNonNull);
else
This = buildLValue(BaseExpr).getAddress();

Expand Down
24 changes: 14 additions & 10 deletions clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "clang/CIR/Dialect/IR/FPEnv.h"
#include "clang/Frontend/FrontendDiagnostic.h"

#include "CIRGenTBAA.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Support/LogicalResult.h"

Expand Down Expand Up @@ -890,22 +891,25 @@ void CIRGenFunction::buildConstructorBody(FunctionArgList &Args) {

/// Given a value of type T* that may not be to a complete object, construct
/// an l-vlaue withi the natural pointee alignment of T.
LValue CIRGenFunction::MakeNaturalAlignPointeeAddrLValue(mlir::Value V,
QualType T) {
LValue CIRGenFunction::MakeNaturalAlignPointeeAddrLValue(mlir::Value val,
QualType ty) {
// FIXME(cir): is it safe to assume Op->getResult(0) is valid? Perhaps
// assert on the result type first.
LValueBaseInfo BaseInfo;
CharUnits Align = CGM.getNaturalTypeAlignment(T, &BaseInfo,
LValueBaseInfo baseInfo;
TBAAAccessInfo tbaaInfo;
CharUnits align = CGM.getNaturalTypeAlignment(ty, &baseInfo, &tbaaInfo,
/* for PointeeType= */ true);
return makeAddrLValue(Address(V, Align), T, BaseInfo);
return makeAddrLValue(Address(val, align), ty, baseInfo);
}

LValue CIRGenFunction::MakeNaturalAlignAddrLValue(mlir::Value V, QualType T) {
LValueBaseInfo BaseInfo;
LValue CIRGenFunction::MakeNaturalAlignAddrLValue(mlir::Value val,
QualType ty) {
LValueBaseInfo baseInfo;
TBAAAccessInfo tbaaInfo;
assert(!MissingFeatures::tbaa());
CharUnits Alignment = CGM.getNaturalTypeAlignment(T, &BaseInfo);
Address Addr(V, getTypes().convertTypeForMem(T), Alignment);
return LValue::makeAddr(Addr, T, getContext(), BaseInfo);
CharUnits alignment = CGM.getNaturalTypeAlignment(ty, &baseInfo, &tbaaInfo);
Address addr(val, getTypes().convertTypeForMem(ty), alignment);
return LValue::makeAddr(addr, ty, getContext(), baseInfo, tbaaInfo);
}

// Map the LangOption for exception behavior into the corresponding enum in
Expand Down
80 changes: 49 additions & 31 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "CIRGenBuilder.h"
#include "CIRGenCall.h"
#include "CIRGenModule.h"
#include "CIRGenTBAA.h"
#include "CIRGenTypeCache.h"
#include "CIRGenValue.h"
#include "EHScopeStack.h"
Expand Down Expand Up @@ -816,12 +817,15 @@ class CIRGenFunction : public CIRGenTypeCache {
/// the address of the lvalue, then loads the result as an rvalue,
/// returning the rvalue.
RValue buildLoadOfLValue(LValue LV, SourceLocation Loc);
mlir::Value buildLoadOfScalar(Address Addr, bool Volatile, clang::QualType Ty,
clang::SourceLocation Loc,
LValueBaseInfo BaseInfo,
mlir::Value buildLoadOfScalar(Address addr, bool isVolatile,
clang::QualType ty, clang::SourceLocation loc,
LValueBaseInfo baseInfo,
TBAAAccessInfo tbaaInfo,
bool isNontemporal = false);
mlir::Value buildLoadOfScalar(Address Addr, bool Volatile, clang::QualType Ty,
mlir::Location Loc, LValueBaseInfo BaseInfo,
mlir::Value buildLoadOfScalar(Address addr, bool isVolatile,
clang::QualType ty, mlir::Location loc,
LValueBaseInfo baseInfo,
TBAAAccessInfo tbaaInfo,
bool isNontemporal = false);

int64_t getAccessedFieldNo(unsigned idx, const mlir::ArrayAttr elts);
Expand All @@ -834,12 +838,12 @@ class CIRGenFunction : public CIRGenTypeCache {

/// Load a scalar value from an address, taking care to appropriately convert
/// from the memory representation to CIR value representation.
mlir::Value buildLoadOfScalar(Address Addr, bool Volatile, clang::QualType Ty,
clang::SourceLocation Loc,
AlignmentSource Source = AlignmentSource::Type,
mlir::Value buildLoadOfScalar(Address addr, bool isVolatile,
clang::QualType ty, clang::SourceLocation loc,
AlignmentSource source = AlignmentSource::Type,
bool isNontemporal = false) {
return buildLoadOfScalar(Addr, Volatile, Ty, Loc, LValueBaseInfo(Source),
isNontemporal);
return buildLoadOfScalar(addr, isVolatile, ty, loc, LValueBaseInfo(source),
CGM.getTBAAAccessInfo(ty), isNontemporal);
}

/// Load a scalar value from an address, taking care to appropriately convert
Expand All @@ -851,8 +855,9 @@ class CIRGenFunction : public CIRGenTypeCache {
/// Load a complex number from the specified l-value.
mlir::Value buildLoadOfComplex(LValue src, SourceLocation loc);

Address buildLoadOfReference(LValue RefLVal, mlir::Location Loc,
LValueBaseInfo *PointeeBaseInfo = nullptr);
Address buildLoadOfReference(LValue refLVal, mlir::Location loc,
LValueBaseInfo *pointeeBaseInfo = nullptr,
TBAAAccessInfo *pointeeTBAAInfo = nullptr);
LValue buildLoadOfReferenceLValue(LValue RefLVal, mlir::Location Loc);
LValue
buildLoadOfReferenceLValue(Address RefAddr, mlir::Location Loc,
Expand Down Expand Up @@ -1275,9 +1280,17 @@ class CIRGenFunction : public CIRGenTypeCache {
clang::QualType::DestructionKind dtorKind);

void buildStoreOfScalar(mlir::Value value, LValue lvalue);
void buildStoreOfScalar(mlir::Value Value, Address Addr, bool Volatile,
clang::QualType Ty, LValueBaseInfo BaseInfo,
bool isInit = false, bool isNontemporal = false);
void buildStoreOfScalar(mlir::Value value, Address addr, bool isVolatile,
clang::QualType ty, LValueBaseInfo baseInfo,
TBAAAccessInfo tbaaInfo, bool isInit = false,
bool isNontemporal = false);
void buildStoreOfScalar(mlir::Value value, Address addr, bool isVolatile,
QualType ty,
AlignmentSource source = AlignmentSource::Type,
bool isInit = false, bool isNontemporal = false) {
buildStoreOfScalar(value, addr, isVolatile, ty, LValueBaseInfo(source),
CGM.getTBAAAccessInfo(ty), isInit, isNontemporal);
}
void buildStoreOfScalar(mlir::Value value, LValue lvalue, bool isInit);

mlir::Value buildToMemory(mlir::Value Value, clang::QualType Ty);
Expand Down Expand Up @@ -1352,9 +1365,10 @@ class CIRGenFunction : public CIRGenTypeCache {
/// reasonable to just ignore the returned alignment when it isn't from an
/// explicit source.
Address
buildPointerWithAlignment(const clang::Expr *E,
LValueBaseInfo *BaseInfo = nullptr,
KnownNonNull_t IsKnownNonNull = NotKnownNonNull);
buildPointerWithAlignment(const clang::Expr *expr,
LValueBaseInfo *baseInfo = nullptr,
TBAAAccessInfo *tbaaInfo = nullptr,
KnownNonNull_t isKnownNonNull = NotKnownNonNull);

LValue
buildConditionalOperatorLValue(const AbstractConditionalOperator *expr);
Expand Down Expand Up @@ -1534,19 +1548,21 @@ class CIRGenFunction : public CIRGenTypeCache {
};

LValue MakeNaturalAlignPointeeAddrLValue(mlir::Value V, clang::QualType T);
LValue MakeNaturalAlignAddrLValue(mlir::Value V, QualType T);
LValue MakeNaturalAlignAddrLValue(mlir::Value val, QualType ty);

/// Construct an address with the natural alignment of T. If a pointer to T
/// is expected to be signed, the pointer passed to this function must have
/// been signed, and the returned Address will have the pointer authentication
/// information needed to authenticate the signed pointer.
Address makeNaturalAddressForPointer(
mlir::Value Ptr, QualType T, CharUnits Alignment = CharUnits::Zero(),
bool ForPointeeType = false, LValueBaseInfo *BaseInfo = nullptr,
KnownNonNull_t IsKnownNonNull = NotKnownNonNull) {
if (Alignment.isZero())
Alignment = CGM.getNaturalTypeAlignment(T, BaseInfo, ForPointeeType);
return Address(Ptr, convertTypeForMem(T), Alignment, IsKnownNonNull);
mlir::Value ptr, QualType t, CharUnits alignment = CharUnits::Zero(),
bool forPointeeType = false, LValueBaseInfo *baseInfo = nullptr,
TBAAAccessInfo *tbaaInfo = nullptr,
KnownNonNull_t isKnownNonNull = NotKnownNonNull) {
if (alignment.isZero())
alignment =
CGM.getNaturalTypeAlignment(t, baseInfo, tbaaInfo, forPointeeType);
return Address(ptr, convertTypeForMem(t), alignment, isKnownNonNull);
}

/// Load the value for 'this'. This function is only valid while generating
Expand Down Expand Up @@ -1590,14 +1606,16 @@ class CIRGenFunction : public CIRGenTypeCache {
QualType DstTy,
SourceLocation Loc);

LValue makeAddrLValue(Address Addr, clang::QualType T,
LValueBaseInfo BaseInfo) {
return LValue::makeAddr(Addr, T, getContext(), BaseInfo);
LValue makeAddrLValue(Address addr, clang::QualType ty,
LValueBaseInfo baseInfo) {
return LValue::makeAddr(addr, ty, getContext(), baseInfo,
CGM.getTBAAAccessInfo(ty));
}

LValue makeAddrLValue(Address Addr, clang::QualType T,
AlignmentSource Source = AlignmentSource::Type) {
return LValue::makeAddr(Addr, T, getContext(), LValueBaseInfo(Source));
LValue makeAddrLValue(Address addr, clang::QualType ty,
AlignmentSource source = AlignmentSource::Type) {
return LValue::makeAddr(addr, ty, getContext(), LValueBaseInfo(source),
CGM.getTBAAAccessInfo(ty));
}

void initializeVTablePointers(mlir::Location loc,
Expand Down
16 changes: 11 additions & 5 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "CIRGenCstEmitter.h"
#include "CIRGenFunction.h"
#include "CIRGenOpenMPRuntime.h"
#include "CIRGenTBAA.h"
#include "CIRGenTypes.h"
#include "CIRGenValue.h"
#include "TargetInfo.h"
Expand Down Expand Up @@ -243,11 +244,9 @@ CharUnits CIRGenModule::getClassPointerAlignment(const CXXRecordDecl *RD) {

/// FIXME: this could likely be a common helper and not necessarily related
/// with codegen.
/// TODO: Add TBAAAccessInfo
CharUnits
CIRGenModule::getNaturalPointeeTypeAlignment(QualType T,
LValueBaseInfo *BaseInfo) {
return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo,
CharUnits CIRGenModule::getNaturalPointeeTypeAlignment(
QualType ty, LValueBaseInfo *baseInfo, TBAAAccessInfo *tbaaInfo) {
return getNaturalTypeAlignment(ty->getPointeeType(), baseInfo, tbaaInfo,
/* forPointeeType= */ true);
}

Expand All @@ -256,6 +255,7 @@ CIRGenModule::getNaturalPointeeTypeAlignment(QualType T,
/// TODO: Add TBAAAccessInfo
CharUnits CIRGenModule::getNaturalTypeAlignment(QualType T,
LValueBaseInfo *BaseInfo,
TBAAAccessInfo *tbaaInfo,
bool forPointeeType) {
// FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown. But
// that doesn't return the information we need to compute BaseInfo.
Expand Down Expand Up @@ -3377,3 +3377,9 @@ void CIRGenModule::buildGlobalAnnotations() {
}
deferredAnnotations.clear();
}

TBAAAccessInfo CIRGenModule::getTBAAAccessInfo(QualType accessType) {
if (!tbaa)
return TBAAAccessInfo();
llvm_unreachable("NYI");
}
16 changes: 12 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "CIRGenBuilder.h"
#include "CIRGenCall.h"
#include "CIRGenOpenCLRuntime.h"
#include "CIRGenTBAA.h"
#include "CIRGenTypeCache.h"
#include "CIRGenTypes.h"
#include "CIRGenVTables.h"
Expand Down Expand Up @@ -95,6 +96,8 @@ class CIRGenModule : public CIRGenTypeCache {

std::unique_ptr<CIRGenCXXABI> ABI;

std::unique_ptr<CIRGenTBAA> tbaa;

/// Used for `UniqueInternalLinkageNames` option
std::string ModuleNameHash = "";

Expand Down Expand Up @@ -442,15 +445,16 @@ class CIRGenModule : public CIRGenTypeCache {

/// FIXME: this could likely be a common helper and not necessarily related
/// with codegen.
/// TODO: Add TBAAAccessInfo
clang::CharUnits getNaturalPointeeTypeAlignment(clang::QualType T,
LValueBaseInfo *BaseInfo);
clang::CharUnits
getNaturalPointeeTypeAlignment(clang::QualType ty,
LValueBaseInfo *baseInfo = nullptr,
TBAAAccessInfo *tbaaInfo = nullptr);

/// FIXME: this could likely be a common helper and not necessarily related
/// with codegen.
/// TODO: Add TBAAAccessInfo
clang::CharUnits getNaturalTypeAlignment(clang::QualType T,
LValueBaseInfo *BaseInfo = nullptr,
TBAAAccessInfo *tbaaInfo = nullptr,
bool forPointeeType = false);

/// TODO: Add TBAAAccessInfo
Expand Down Expand Up @@ -482,6 +486,10 @@ class CIRGenModule : public CIRGenTypeCache {
return VTables.getItaniumVTableContext();
}

/// getTBAAAccessInfo - Gte TBAA information that describes an access to an
/// object of the given type.
TBAAAccessInfo getTBAAAccessInfo(QualType accessType);

/// This contains all the decls which have definitions but which are deferred
/// for emission and therefore should only be output if they are actually
/// used. If a decl is in this, then it is known to have not been referenced
Expand Down
28 changes: 28 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenTBAA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===--- CIRGenTBAA.h - TBAA information for LLVM CIRGen --------*- 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 is the code that manages TBAA information and defines the TBAA policy
// for the optimizer to use.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENTBAA_H
#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENTBAA_H

namespace cir {

// TBAAAccessInfo - Describes a memory access in terms of TBAA.
struct TBAAAccessInfo {};

/// CIRGenTBAA - This class organizes the cross-module state that is used while
/// lowering AST types to LLVM types.
class CIRGenTBAA {};

} // namespace cir

#endif
Loading

0 comments on commit 9a85975

Please sign in to comment.