From 6b829fafa35355994390c6930562146482383abc Mon Sep 17 00:00:00 2001 From: gitoleg Date: Thu, 25 Jan 2024 17:56:12 +0300 Subject: [PATCH] changes after review --- clang/lib/CIR/CodeGen/CIRGenDecl.cpp | 46 +++++++++++-------- clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 25 ++-------- clang/lib/CIR/CodeGen/CIRGenFunction.h | 2 +- .../CodeGen/UnimplementedFeatureGuarding.h | 1 + 4 files changed, 33 insertions(+), 41 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp index 8d3802047e16..35346a0560cd 100644 --- a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp @@ -151,33 +151,41 @@ CIRGenFunction::buildAutoVarAlloca(const VarDecl &D) { assert(!UnimplementedFeature::shouldEmitLifetimeMarkers()); } } else { // not openmp nor constant sized type + bool VarAllocated = false; if (getLangOpts().OpenMPIsTargetDevice) llvm_unreachable("NYI"); - if (!DidCallStackSave) { - // Save the stack. - auto defaultTy = AllocaInt8PtrTy; - CharUnits Align = CharUnits::fromQuantity( - CGM.getDataLayout().getAlignment(defaultTy, false)); - Address Stack = CreateTempAlloca(defaultTy, Align, loc, "saved_stack"); + if (!VarAllocated) { + if (!DidCallStackSave) { + // Save the stack. + auto defaultTy = AllocaInt8PtrTy; + CharUnits Align = CharUnits::fromQuantity( + CGM.getDataLayout().getAlignment(defaultTy, false)); + Address Stack = CreateTempAlloca(defaultTy, Align, loc, "saved_stack"); - mlir::Value V = builder.createStackSave(loc, defaultTy); - assert(V.getType() == AllocaInt8PtrTy); - builder.createStore(loc, V, Stack); + mlir::Value V = builder.createStackSave(loc, defaultTy); + assert(V.getType() == AllocaInt8PtrTy); + builder.createStore(loc, V, Stack); - DidCallStackSave = true; + DidCallStackSave = true; - // Push a cleanup block and restore the stack there. - // FIXME: in general circumstances, this should be an EH cleanup. - pushStackRestore(NormalCleanup, Stack); - } + // Push a cleanup block and restore the stack there. + // FIXME: in general circumstances, this should be an EH cleanup. + pushStackRestore(NormalCleanup, Stack); + } - auto VlaSize = getVLASize(Ty); - mlir::Type mTy = convertTypeForMem(VlaSize.Type); + auto VlaSize = getVLASize(Ty); + mlir::Type mTy = convertTypeForMem(VlaSize.Type); + + // Allocate memory for the array. + address = CreateTempAlloca(mTy, alignment, loc, "vla", VlaSize.NumElts, + &allocaAddr, builder.saveInsertionPoint()); + } - // Allocate memory for the array. - address = CreateTempAlloca(mTy, alignment, loc, "vla", VlaSize.NumElts, - &allocaAddr, builder.saveInsertionPoint()); + // If we have debug info enabled, properly describe the VLA dimensions for + // this type by registering the vla size expression for each of the + // dimensions. + assert(!UnimplementedFeature::generateDebugInfo()); } emission.Addr = address; diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp index 937c35f40cc0..651f97d538cb 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp @@ -1423,9 +1423,12 @@ CIRGenFunction::getVLASize(const VariableArrayType *type) { } } while ((type = getContext().getAsVariableArrayType(elementType))); + assert(numElements && "Undefined elements number"); return {numElements, elementType}; } +// TODO(cir): most part of this function can be shared between CIRGen +// and traditional LLVM codegen void CIRGenFunction::buildVariablyModifiedType(QualType type) { assert(type->isVariablyModifiedType() && "Must pass variably modified type to EmitVLASizes!"); @@ -1510,27 +1513,7 @@ void CIRGenFunction::buildVariablyModifiedType(QualType type) { mlir::Value &entry = VLASizeMap[sizeExpr]; if (!entry) { mlir::Value size = buildScalarExpr(sizeExpr); - - // // C11 6.7.6.2p5: - // // If the size is an expression that is not an integer constant - // // expression [...] each time it is evaluated it shall have a - // // value greater than zero. - // if (SanOpts.has(SanitizerKind::VLABound)) { - // SanitizerScope SanScope(this); - // llvm::Value *Zero = - // llvm::Constant::getNullValue(size->getType()); clang::QualType - // SEType = sizeExpr->getType(); llvm::Value *CheckCondition = - // SEType->isSignedIntegerType() - // ? Builder.CreateICmpSGT(size, Zero) - // : Builder.CreateICmpUGT(size, Zero); - // llvm::Constant *StaticArgs[] = { - // EmitCheckSourceLocation(sizeExpr->getBeginLoc()), - // EmitCheckTypeDescriptor(SEType)}; - // EmitCheck(std::make_pair(CheckCondition, - // SanitizerKind::VLABound), - // SanitizerHandler::VLABoundNotPositive, StaticArgs, - // size); - // } + assert(!UnimplementedFeature::sanitizeVLABound()); // Always zexting here would be wrong if it weren't // undefined behavior to have a negative bound. diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h index 45b105c4b4ff..c2cf82c72c6c 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.h +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h @@ -93,7 +93,7 @@ class CIRGenFunction : public CIRGenTypeCache { llvm::DenseMap OpaqueLValues; llvm::DenseMap OpaqueRValues; - // VLASizeMap - This keeps track of the associated size for each VLA type. + // This keeps track of the associated size for each VLA type. // We track this by the size expression rather than the type itself because // in certain situations, like a const qualifier applied to an VLA typedef, // multiple VLA types can share the same size expression. diff --git a/clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h b/clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h index 1e5d1dfe7526..f38b2ff7d523 100644 --- a/clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h +++ b/clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h @@ -58,6 +58,7 @@ struct UnimplementedFeature { static bool emitCheckedInBoundsGEP() { return false; } static bool pointerOverflowSanitizer() { return false; } static bool sanitizeDtor() { return false; } + static bool sanitizeVLABound() { return false; } // ObjC static bool setObjCGCLValueClass() { return false; }