Skip to content

Commit f725587

Browse files
authored
Merge branch 'main' into python-limited-api-typemaps
2 parents 798f5d4 + 21502bd commit f725587

File tree

24 files changed

+365
-177
lines changed

24 files changed

+365
-177
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,10 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
315315
return getConstantInt(loc, getUInt32Ty(), c);
316316
}
317317
cir::ConstantOp getSInt64(uint64_t c, mlir::Location loc) {
318-
cir::IntType sInt64Ty = getSInt64Ty();
319-
return cir::ConstantOp::create(*this, loc, cir::IntAttr::get(sInt64Ty, c));
318+
return getConstantInt(loc, getSInt64Ty(), c);
319+
}
320+
cir::ConstantOp getUInt64(uint64_t c, mlir::Location loc) {
321+
return getConstantInt(loc, getUInt64Ty(), c);
320322
}
321323

322324
mlir::Value createNeg(mlir::Value value) {

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,22 @@ CIRGenFunction::emitTargetBuiltinExpr(unsigned builtinID, const CallExpr *e,
630630
getTarget().getTriple().getArch());
631631
}
632632

633+
mlir::Value CIRGenFunction::emitScalarOrConstFoldImmArg(
634+
const unsigned iceArguments, const unsigned idx, const Expr *argExpr) {
635+
mlir::Value arg = {};
636+
if ((iceArguments & (1 << idx)) == 0) {
637+
arg = emitScalarExpr(argExpr);
638+
} else {
639+
// If this is required to be a constant, constant fold it so that we
640+
// know that the generated intrinsic gets a ConstantInt.
641+
const std::optional<llvm::APSInt> result =
642+
argExpr->getIntegerConstantExpr(getContext());
643+
assert(result && "Expected argument to be a constant");
644+
arg = builder.getConstInt(getLoc(argExpr->getSourceRange()), *result);
645+
}
646+
return arg;
647+
}
648+
633649
/// Given a builtin id for a function like "__builtin_fabsf", return a Function*
634650
/// for "fabsf".
635651
cir::FuncOp CIRGenModule::getBuiltinLibFunction(const FunctionDecl *fd,

clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "clang/Basic/Builtins.h"
1717
#include "clang/Basic/TargetBuiltins.h"
1818
#include "clang/CIR/MissingFeatures.h"
19-
#include "llvm/IR/IntrinsicsX86.h"
2019

2120
using namespace clang;
2221
using namespace clang::CIRGen;
@@ -66,9 +65,8 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
6665
getContext().GetBuiltinType(builtinID, error, &iceArguments);
6766
assert(error == ASTContext::GE_None && "Error while getting builtin type.");
6867

69-
for (auto [idx, arg] : llvm::enumerate(e->arguments())) {
68+
for (auto [idx, arg] : llvm::enumerate(e->arguments()))
7069
ops.push_back(emitScalarOrConstFoldImmArg(iceArguments, idx, arg));
71-
}
7270

7371
CIRGenBuilderTy &builder = getBuilder();
7472
mlir::Type voidTy = builder.getVoidTy();
@@ -98,6 +96,10 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
9896
case X86::BI__builtin_ia32_undef128:
9997
case X86::BI__builtin_ia32_undef256:
10098
case X86::BI__builtin_ia32_undef512:
99+
cgm.errorNYI(e->getSourceRange(),
100+
std::string("unimplemented X86 builtin call: ") +
101+
getContext().BuiltinInfo.getName(builtinID));
102+
return {};
101103
case X86::BI__builtin_ia32_vec_ext_v4hi:
102104
case X86::BI__builtin_ia32_vec_ext_v16qi:
103105
case X86::BI__builtin_ia32_vec_ext_v8hi:
@@ -107,7 +109,22 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
107109
case X86::BI__builtin_ia32_vec_ext_v32qi:
108110
case X86::BI__builtin_ia32_vec_ext_v16hi:
109111
case X86::BI__builtin_ia32_vec_ext_v8si:
110-
case X86::BI__builtin_ia32_vec_ext_v4di:
112+
case X86::BI__builtin_ia32_vec_ext_v4di: {
113+
unsigned numElts = cast<cir::VectorType>(ops[0].getType()).getSize();
114+
115+
uint64_t index =
116+
ops[1].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue();
117+
118+
index &= numElts - 1;
119+
120+
cir::ConstantOp indexVal =
121+
builder.getUInt64(index, getLoc(e->getExprLoc()));
122+
123+
// These builtins exist so we can ensure the index is an ICE and in range.
124+
// Otherwise we could just do this in the header file.
125+
return cir::VecExtractOp::create(builder, getLoc(e->getExprLoc()), ops[0],
126+
indexVal);
127+
}
111128
case X86::BI__builtin_ia32_vec_set_v4hi:
112129
case X86::BI__builtin_ia32_vec_set_v16qi:
113130
case X86::BI__builtin_ia32_vec_set_v8hi:

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,28 +1442,6 @@ mlir::Value CIRGenFunction::emitPromotedScalarExpr(const Expr *e,
14421442
return ScalarExprEmitter(*this, builder).Visit(const_cast<Expr *>(e));
14431443
}
14441444

1445-
mlir::Value CIRGenFunction::emitScalarOrConstFoldImmArg(unsigned iceArguments,
1446-
unsigned index,
1447-
const Expr *arg) {
1448-
mlir::Value result{};
1449-
1450-
// The bit at the specified index indicates whether the argument is required
1451-
// to be a constant integer expression.
1452-
bool isArgRequiredToBeConstant = (iceArguments & (1 << index));
1453-
1454-
if (!isArgRequiredToBeConstant) {
1455-
result = emitScalarExpr(arg);
1456-
} else {
1457-
// If this is required to be a constant, constant fold it so that we
1458-
// know that the generated intrinsic gets a ConstantInt.
1459-
std::optional<llvm::APSInt> iceOpt =
1460-
arg->getIntegerConstantExpr(getContext());
1461-
assert(iceOpt && "Expected argument to be a constant");
1462-
result = builder.getConstInt(getLoc(arg->getSourceRange()), *iceOpt);
1463-
}
1464-
return result;
1465-
}
1466-
14671445
[[maybe_unused]] static bool mustVisitNullValue(const Expr *e) {
14681446
// If a null pointer expression's type is the C++0x nullptr_t and
14691447
// the expression is not a simple literal, it must be evaluated

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,9 +1546,6 @@ class CIRGenFunction : public CIRGenTypeCache {
15461546
mlir::Value emitScalarExpr(const clang::Expr *e,
15471547
bool ignoreResultAssign = false);
15481548

1549-
mlir::Value emitScalarOrConstFoldImmArg(unsigned iceArguments, unsigned index,
1550-
const Expr *arg);
1551-
15521549
mlir::Value emitScalarPrePostIncDec(const UnaryOperator *e, LValue lv,
15531550
cir::UnaryOpKind kind, bool isPre);
15541551

@@ -1721,6 +1718,9 @@ class CIRGenFunction : public CIRGenTypeCache {
17211718
void emitScalarInit(const clang::Expr *init, mlir::Location loc,
17221719
LValue lvalue, bool capturedByInit = false);
17231720

1721+
mlir::Value emitScalarOrConstFoldImmArg(unsigned iceArguments, unsigned idx,
1722+
const Expr *argExpr);
1723+
17241724
void emitStaticVarDecl(const VarDecl &d, cir::GlobalLinkageKind linkage);
17251725

17261726
void emitStoreOfComplex(mlir::Location loc, mlir::Value v, LValue dest,

clang/test/CIR/CodeGen/X86/sse2-builtins.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616

1717
#include <immintrin.h>
1818

19+
// Lowering to pextrw requires optimization.
20+
int test_mm_extract_epi16(__m128i A) {
21+
// CIR-LABEL: test_mm_extract_epi16
22+
// CIR %{{.*}} = cir.vec.extract %{{.*}}[%{{.*}} : {{!u32i|!u64i}}] : !cir.vector<!s16i x 8>
23+
// CIR %{{.*}} = cir.cast integral %{{.*}} : !u16i -> !s32i
24+
25+
// LLVM-LABEL: test_mm_extract_epi16
26+
// LLVM: extractelement <8 x i16> %{{.*}}, {{i32|i64}} 1
27+
// LLVM: zext i16 %{{.*}} to i32
28+
29+
// OGCG-LABEL: test_mm_extract_epi16
30+
// OGCG: extractelement <8 x i16> %{{.*}}, {{i32|i64}} 1
31+
// OGCG: zext i16 %{{.*}} to i32
32+
return _mm_extract_epi16(A, 1);
33+
}
1934

2035
void test_mm_clflush(void* A) {
2136
// CIR-LABEL: test_mm_clflush

lldb/bindings/python/python-typemaps.swig

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -628,61 +628,34 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
628628
}
629629
}
630630

631-
// These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i,
632-
// and fixed so they will not crash if PyObject_GetBuffer fails.
633-
// https://github.com/swig/swig/issues/1640
634-
//
635-
// I've also moved the call to PyBuffer_Release to the end of the SWIG wrapper,
636-
// doing it right away is not legal according to the python buffer protocol.
637-
%inline %{
638-
struct Py_buffer_RAII {
639-
Py_buffer buffer = {};
640-
Py_buffer_RAII(){};
641-
Py_buffer &operator=(const Py_buffer_RAII &) = delete;
642-
Py_buffer_RAII(const Py_buffer_RAII &) = delete;
643-
~Py_buffer_RAII() {
644-
if (buffer.obj)
645-
PyBuffer_Release(&buffer);
646-
}
647-
};
648-
%}
649631

650-
%define %pybuffer_mutable_binary(TYPEMAP, SIZE)
651-
%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) {
652-
int res;
653-
Py_ssize_t size = 0;
654-
void *buf = 0;
655-
res = PyObject_GetBuffer($input, &view.buffer, PyBUF_WRITABLE);
656-
if (res < 0) {
657-
PyErr_Clear();
658-
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
659-
}
660-
size = view.buffer.len;
661-
buf = view.buffer.buf;
662-
$1 = ($1_ltype)buf;
663-
$2 = ($2_ltype)(size / sizeof($*1_type));
664-
}
665-
%enddef
666-
667-
%define %pybuffer_binary(TYPEMAP, SIZE)
668-
%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) {
669-
int res;
670-
Py_ssize_t size = 0;
671-
const void *buf = 0;
672-
res = PyObject_GetBuffer($input, &view.buffer, PyBUF_CONTIG_RO);
673-
if (res < 0) {
674-
PyErr_Clear();
675-
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
632+
// Typemap for SBFile::Write.
633+
%typemap(in) (const uint8_t *buf, size_t num_bytes) {
634+
if (PythonByteArray::Check($input)) {
635+
PythonByteArray bytearray(PyRefType::Borrowed, $input);
636+
$1 = (uint8_t *)bytearray.GetBytes().data();
637+
$2 = bytearray.GetSize();
638+
} else if (PythonBytes::Check($input)) {
639+
PythonBytes bytes(PyRefType::Borrowed, $input);
640+
$1 = (uint8_t *)bytes.GetBytes().data();
641+
$2 = bytes.GetSize();
642+
} else {
643+
PyErr_SetString(PyExc_ValueError, "Expecting a bytes or bytearray object");
644+
SWIG_fail;
676645
}
677-
size = view.buffer.len;
678-
buf = view.buffer.buf;
679-
$1 = ($1_ltype)buf;
680-
$2 = ($2_ltype)(size / sizeof($*1_type));
681646
}
682-
%enddef
683647

684-
%pybuffer_binary(const uint8_t *buf, size_t num_bytes);
685-
%pybuffer_mutable_binary(uint8_t *buf, size_t num_bytes);
648+
// Typemap for SBFile::Read.
649+
%typemap(in) (uint8_t *buf, size_t num_bytes) {
650+
if (PythonByteArray::Check($input)) {
651+
PythonByteArray bytearray(PyRefType::Borrowed, $input);
652+
$1 = (uint8_t *)bytearray.GetBytes().data();
653+
$2 = bytearray.GetSize();
654+
} else {
655+
PyErr_SetString(PyExc_ValueError, "Expecting a bytearray");
656+
SWIG_fail;
657+
}
658+
}
686659

687660
%typemap(in) (const char **symbol_name, uint32_t num_names) {
688661
using namespace lldb_private;

lldb/cmake/modules/LLDBConfig.cmake

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,25 @@ if (LLDB_ENABLE_PYTHON)
180180
"Path to use as PYTHONHOME in lldb. If a relative path is specified, it will be resolved at runtime relative to liblldb directory.")
181181
endif()
182182

183-
if (SWIG_VERSION VERSION_GREATER_EQUAL "4.2" AND NOT LLDB_EMBED_PYTHON_HOME)
183+
# Enable targeting the Python Limited C API.
184+
set(PYTHON_LIMITED_API_MIN_SWIG_VERSION "4.2")
185+
if (SWIG_VERSION VERSION_GREATER_EQUAL PYTHON_LIMITED_API_MIN_SWIG_VERSION
186+
AND NOT LLDB_EMBED_PYTHON_HOME)
184187
set(default_enable_python_limited_api ON)
185188
else()
186189
set(default_enable_python_limited_api OFF)
187190
endif()
188191
option(LLDB_ENABLE_PYTHON_LIMITED_API "Force LLDB to only use the Python Limited API (requires SWIG 4.2 or later)"
189192
${default_enable_python_limited_api})
193+
194+
# Diagnose unsupported configurations.
195+
if (LLDB_ENABLE_PYTHON_LIMITED_API AND LLDB_EMBED_PYTHON_HOME)
196+
message(SEND_ERROR "LLDB_ENABLE_PYTHON_LIMITED_API is not compatible with LLDB_EMBED_PYTHON_HOME")
197+
endif()
198+
if (LLDB_ENABLE_PYTHON_LIMITED_API AND SWIG_VERSION VERSION_LESS PYTHON_LIMITED_API_MIN_SWIG_VERSION)
199+
message(SEND_ERROR "LLDB_ENABLE_PYTHON_LIMITED_API is not compatible with SWIG ${SWIG_VERSION} (requires SWIG ${PYTHON_LIMITED_API_MIN_SWIG_VERSION})")
200+
endif()
201+
190202
else()
191203
# Even if Python scripting is disabled, we still need a Python interpreter to
192204
# build, for example to generate SBLanguages.h.

lldb/include/lldb/Core/ModuleList.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,12 @@ class ModuleList {
511511
/// Atomically swaps the contents of this module list with \a other.
512512
void Swap(ModuleList &other);
513513

514+
/// For each module in this ModuleList, preload its symbols.
515+
///
516+
/// \param[in] parallelize
517+
/// If true, all modules will be preloaded in parallel.
518+
void PreloadSymbols(bool parallelize) const;
519+
514520
protected:
515521
// Class typedefs.
516522
typedef std::vector<lldb::ModuleSP>

lldb/include/lldb/Target/DynamicLoader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ class DynamicLoader : public PluginInterface {
352352
protected:
353353
// Utility methods for derived classes
354354

355+
/// Find a module in the target that matches the given file.
355356
lldb::ModuleSP FindModuleViaTarget(const FileSpec &file);
356357

357358
/// Checks to see if the target module has changed, updates the target

0 commit comments

Comments
 (0)