Skip to content

Commit 1c2506e

Browse files
authored
merge main into amd-staging (llvm#1307)
2 parents f0e500a + 4382836 commit 1c2506e

32 files changed

+442
-332
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3518,7 +3518,7 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
35183518
// ++Iter;
35193519
if (!this->emitGetPtrLocal(Iter, E))
35203520
return false;
3521-
if (!this->emitIncPop(SizeT, E))
3521+
if (!this->emitIncPop(SizeT, false, E))
35223522
return false;
35233523

35243524
if (!this->jump(StartLabel))
@@ -5957,7 +5957,8 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
59575957
: this->emitIncf(getFPOptions(E), E);
59585958
}
59595959

5960-
return DiscardResult ? this->emitIncPop(*T, E) : this->emitInc(*T, E);
5960+
return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E)
5961+
: this->emitInc(*T, E->canOverflow(), E);
59615962
}
59625963
case UO_PostDec: { // x--
59635964
if (!Ctx.getLangOpts().CPlusPlus14)
@@ -5980,7 +5981,8 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
59805981
: this->emitDecf(getFPOptions(E), E);
59815982
}
59825983

5983-
return DiscardResult ? this->emitDecPop(*T, E) : this->emitDec(*T, E);
5984+
return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E)
5985+
: this->emitDec(*T, E->canOverflow(), E);
59845986
}
59855987
case UO_PreInc: { // ++x
59865988
if (!Ctx.getLangOpts().CPlusPlus14)
@@ -6005,7 +6007,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60056007
if (DiscardResult) {
60066008
if (T == PT_Float)
60076009
return this->emitIncfPop(getFPOptions(E), E);
6008-
return this->emitIncPop(*T, E);
6010+
return this->emitIncPop(*T, E->canOverflow(), E);
60096011
}
60106012

60116013
if (T == PT_Float) {
@@ -6020,13 +6022,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60206022
return false;
60216023
} else {
60226024
assert(isIntegralType(*T));
6023-
if (!this->emitLoad(*T, E))
6024-
return false;
6025-
if (!this->emitConst(1, E))
6026-
return false;
6027-
if (!this->emitAdd(*T, E))
6028-
return false;
6029-
if (!this->emitStore(*T, E))
6025+
if (!this->emitPreInc(*T, E->canOverflow(), E))
60306026
return false;
60316027
}
60326028
return E->isGLValue() || this->emitLoadPop(*T, E);
@@ -6054,7 +6050,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60546050
if (DiscardResult) {
60556051
if (T == PT_Float)
60566052
return this->emitDecfPop(getFPOptions(E), E);
6057-
return this->emitDecPop(*T, E);
6053+
return this->emitDecPop(*T, E->canOverflow(), E);
60586054
}
60596055

60606056
if (T == PT_Float) {
@@ -6069,13 +6065,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60696065
return false;
60706066
} else {
60716067
assert(isIntegralType(*T));
6072-
if (!this->emitLoad(*T, E))
6073-
return false;
6074-
if (!this->emitConst(1, E))
6075-
return false;
6076-
if (!this->emitSub(*T, E))
6077-
return false;
6078-
if (!this->emitStore(*T, E))
6068+
if (!this->emitPreDec(*T, E->canOverflow(), E))
60796069
return false;
60806070
}
60816071
return E->isGLValue() || this->emitLoadPop(*T, E);

clang/lib/AST/ByteCode/Interp.h

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,8 @@ enum class IncDecOp {
765765
};
766766

767767
template <typename T, IncDecOp Op, PushVal DoPush>
768-
bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
768+
bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
769+
bool CanOverflow) {
769770
assert(!Ptr.isDummy());
770771

771772
if constexpr (std::is_same_v<T, Boolean>) {
@@ -780,16 +781,17 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
780781
S.Stk.push<T>(Value);
781782

782783
if constexpr (Op == IncDecOp::Inc) {
783-
if (!T::increment(Value, &Result)) {
784+
if (!T::increment(Value, &Result) || !CanOverflow) {
784785
Ptr.deref<T>() = Result;
785786
return true;
786787
}
787788
} else {
788-
if (!T::decrement(Value, &Result)) {
789+
if (!T::decrement(Value, &Result) || !CanOverflow) {
789790
Ptr.deref<T>() = Result;
790791
return true;
791792
}
792793
}
794+
assert(CanOverflow);
793795

794796
// Something went wrong with the previous operation. Compute the
795797
// result with another bit of precision.
@@ -812,7 +814,6 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
812814
<< Trunc << Type << E->getSourceRange();
813815
return true;
814816
}
815-
816817
return handleOverflow(S, OpPC, APResult);
817818
}
818819

@@ -821,49 +822,69 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
821822
/// 3) Writes the value increased by one back to the pointer
822823
/// 4) Pushes the original (pre-inc) value on the stack.
823824
template <PrimType Name, class T = typename PrimConv<Name>::T>
824-
bool Inc(InterpState &S, CodePtr OpPC) {
825+
bool Inc(InterpState &S, CodePtr OpPC, bool CanOverflow) {
825826
const Pointer &Ptr = S.Stk.pop<Pointer>();
826827
if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
827828
return false;
828829

829-
return IncDecHelper<T, IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr);
830+
return IncDecHelper<T, IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr,
831+
CanOverflow);
830832
}
831833

832834
/// 1) Pops a pointer from the stack
833835
/// 2) Load the value from the pointer
834836
/// 3) Writes the value increased by one back to the pointer
835837
template <PrimType Name, class T = typename PrimConv<Name>::T>
836-
bool IncPop(InterpState &S, CodePtr OpPC) {
838+
bool IncPop(InterpState &S, CodePtr OpPC, bool CanOverflow) {
837839
const Pointer &Ptr = S.Stk.pop<Pointer>();
838840
if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
839841
return false;
840842

841-
return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr);
843+
return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, CanOverflow);
844+
}
845+
846+
template <PrimType Name, class T = typename PrimConv<Name>::T>
847+
bool PreInc(InterpState &S, CodePtr OpPC, bool CanOverflow) {
848+
const Pointer &Ptr = S.Stk.peek<Pointer>();
849+
if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
850+
return false;
851+
852+
return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, CanOverflow);
842853
}
843854

844855
/// 1) Pops a pointer from the stack
845856
/// 2) Load the value from the pointer
846857
/// 3) Writes the value decreased by one back to the pointer
847858
/// 4) Pushes the original (pre-dec) value on the stack.
848859
template <PrimType Name, class T = typename PrimConv<Name>::T>
849-
bool Dec(InterpState &S, CodePtr OpPC) {
860+
bool Dec(InterpState &S, CodePtr OpPC, bool CanOverflow) {
850861
const Pointer &Ptr = S.Stk.pop<Pointer>();
851862
if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
852863
return false;
853864

854-
return IncDecHelper<T, IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr);
865+
return IncDecHelper<T, IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr,
866+
CanOverflow);
855867
}
856868

857869
/// 1) Pops a pointer from the stack
858870
/// 2) Load the value from the pointer
859871
/// 3) Writes the value decreased by one back to the pointer
860872
template <PrimType Name, class T = typename PrimConv<Name>::T>
861-
bool DecPop(InterpState &S, CodePtr OpPC) {
873+
bool DecPop(InterpState &S, CodePtr OpPC, bool CanOverflow) {
862874
const Pointer &Ptr = S.Stk.pop<Pointer>();
863875
if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
864876
return false;
865877

866-
return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr);
878+
return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, CanOverflow);
879+
}
880+
881+
template <PrimType Name, class T = typename PrimConv<Name>::T>
882+
bool PreDec(InterpState &S, CodePtr OpPC, bool CanOverflow) {
883+
const Pointer &Ptr = S.Stk.peek<Pointer>();
884+
if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
885+
return false;
886+
887+
return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, CanOverflow);
867888
}
868889

869890
template <IncDecOp Op, PushVal DoPush>

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,18 @@ def Shr : Opcode {
593593
def Inv: Opcode;
594594

595595
// Increment and decrement.
596-
def Inc: AluOpcode;
597-
def IncPop : AluOpcode;
598-
def Dec: AluOpcode;
599-
def DecPop: AluOpcode;
596+
class OverflowOpcode : Opcode {
597+
let Types = [AluTypeClass];
598+
let Args = [ArgBool];
599+
let HasGroup = 1;
600+
}
601+
602+
def Inc : OverflowOpcode;
603+
def IncPop : OverflowOpcode;
604+
def PreInc : OverflowOpcode;
605+
def Dec : OverflowOpcode;
606+
def DecPop : OverflowOpcode;
607+
def PreDec : OverflowOpcode;
600608

601609
// Float increment and decrement.
602610
def Incf: FloatOpcode;

clang/test/AST/ByteCode/literals.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,32 @@ namespace IncDec {
598598
static_assert(UnderFlow() == -1, ""); // both-error {{not an integral constant expression}} \
599599
// both-note {{in call to 'UnderFlow()'}}
600600

601+
/// This UnaryOperator can't overflow, so we shouldn't diagnose any overflow.
602+
constexpr int CanOverflow() {
603+
char c = 127;
604+
char p;
605+
++c;
606+
c++;
607+
p = ++c;
608+
p = c++;
609+
610+
c = -128;
611+
--c;
612+
c--;
613+
p = --c;
614+
p = ++c;
615+
616+
return 0;
617+
}
618+
static_assert(CanOverflow() == 0, "");
619+
620+
constexpr char OverflownChar() {
621+
char c = 127;
622+
c++;
623+
return c;
624+
}
625+
static_assert(OverflownChar() == -128, "");
626+
601627
constexpr int getTwo() {
602628
int i = 1;
603629
return (i += 1);

libc/docs/uefi/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ libc for UEFI
88

99
The *UEFI* support for LLVM's libc project aims to provide a standard libc
1010
frontend to the UEFI protocols. This allows for many existing
11-
applications to be easily ported to UEFI. Nagivate using the links below to
11+
applications to be easily ported to UEFI. Navigate using the links below to
1212
learn more about this project.
1313

1414
.. toctree::

llvm/bindings/ocaml/llvm/llvm.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ external global_copy_all_metadata : llvalue -> (llmdkind * llmetadata) array
700700
external is_global_constant : llvalue -> bool = "llvm_is_global_constant"
701701
external set_global_constant : bool -> llvalue -> unit
702702
= "llvm_set_global_constant"
703+
external global_set_metadata : llvalue -> llmdkind -> llmetadata -> unit
704+
= "llvm_global_set_metadata"
703705

704706
(*--... Operations on global variables .....................................--*)
705707
external declare_global : lltype -> string -> llmodule -> llvalue

llvm/bindings/ocaml/llvm/llvm.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,12 @@ val is_global_constant : llvalue -> bool
13581358
See the method [llvm::GlobalVariable::setConstant]. *)
13591359
val set_global_constant : bool -> llvalue -> unit
13601360

1361+
(** [global_set_metadata g k md] sets the metadata attachment of the global
1362+
value [g] to the metadata [md] for the given kind [k], erasing the existing
1363+
metadata attachment if it already exists for the given kind.
1364+
See the method [llvm::GlobalObject::setMetadata]. *)
1365+
val global_set_metadata : llvalue -> llmdkind -> llmetadata -> unit
1366+
13611367
(** [global_initializer gv] If global variable [gv] has an initializer it is returned,
13621368
otherwise returns [None]. See the method [llvm::GlobalVariable::getInitializer]. *)
13631369
val global_initializer : llvalue -> llvalue option

llvm/bindings/ocaml/llvm/llvm_ocaml.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
|* *|
1616
\*===----------------------------------------------------------------------===*/
1717

18-
#include <assert.h>
19-
#include <stdlib.h>
20-
#include <string.h>
18+
#include "llvm_ocaml.h"
19+
#include "caml/callback.h"
20+
#include "caml/fail.h"
21+
#include "caml/memory.h"
2122
#include "llvm-c/Core.h"
2223
#include "llvm-c/Support.h"
2324
#include "llvm/Config/llvm-config.h"
24-
#include "caml/memory.h"
25-
#include "caml/fail.h"
26-
#include "caml/callback.h"
27-
#include "llvm_ocaml.h"
25+
#include <assert.h>
26+
#include <stdlib.h>
27+
#include <string.h>
2828

2929
#if OCAML_VERSION < 41200
3030
value caml_alloc_some(value v) {
@@ -1546,6 +1546,14 @@ value llvm_set_global_constant(value Flag, value GlobalVar) {
15461546
return Val_unit;
15471547
}
15481548

1549+
/* llvalue -> llmdkind -> llmetadata -> unit */
1550+
value llvm_global_set_metadata(value Value, value MetadataKind,
1551+
value Metadata) {
1552+
LLVMGlobalSetMetadata(Value_val(Value), (unsigned int)Int_val(MetadataKind),
1553+
Metadata_val(Metadata));
1554+
return Val_unit;
1555+
}
1556+
15491557
/*--... Operations on aliases ..............................................--*/
15501558

15511559
/* llmodule -> lltype -> int -> llvalue -> string -> llvalue */

llvm/include/llvm/Analysis/EphemeralValuesCache.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,35 @@ class Function;
2323
class AssumptionCache;
2424
class Value;
2525

26+
/// A cache of ephemeral values within a function.
27+
class EphemeralValuesCache {
28+
SmallPtrSet<const Value *, 32> EphValues;
29+
Function &F;
30+
AssumptionCache &AC;
31+
bool Collected = false;
32+
33+
void collectEphemeralValues();
34+
35+
public:
36+
EphemeralValuesCache(Function &F, AssumptionCache &AC) : F(F), AC(AC) {}
37+
void clear() {
38+
EphValues.clear();
39+
Collected = false;
40+
}
41+
const SmallPtrSetImpl<const Value *> &ephValues() {
42+
if (!Collected)
43+
collectEphemeralValues();
44+
return EphValues;
45+
}
46+
};
47+
2648
class EphemeralValuesAnalysis
2749
: public AnalysisInfoMixin<EphemeralValuesAnalysis> {
2850
friend AnalysisInfoMixin<EphemeralValuesAnalysis>;
2951
static AnalysisKey Key;
3052

3153
public:
32-
using Result = SmallPtrSet<const Value *, 32>;
54+
using Result = EphemeralValuesCache;
3355
Result run(Function &F, FunctionAnalysisManager &FAM);
3456
};
3557

0 commit comments

Comments
 (0)