Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/actions/build_cmake/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ inputs:
description: 'Enable SVS support.'
required: false
default: OFF
warning_level:
description: 'Compiler warning level (0=off, 1=basic, 2=standard, 3=strict).'
required: false
default: '0'
warnings_as_errors:
description: 'Treat compiler warnings as errors.'
required: false
default: 'false'
setup_conda:
description: 'Setup miniconda environment.'
required: false
Expand Down Expand Up @@ -150,6 +158,8 @@ runs:
-DFAISS_OPT_LEVEL=${{ inputs.opt_level }} \
-DFAISS_ENABLE_SVS=${{ inputs.svs }} \
-DFAISS_ENABLE_C_API=ON \
-DFAISS_WARNING_LEVEL=${{ inputs.warning_level }} \
-DFAISS_WARNINGS_AS_ERRORS=${{ inputs.warnings_as_errors == 'true' && 'ON' || 'OFF' }} \
-DPYTHON_EXECUTABLE=$CONDA/bin/python \
-DCMAKE_BUILD_TYPE=Release \
-DBLA_VENDOR=${{ runner.arch == 'X64' && 'Intel10_64_dyn' || '' }} \
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/build-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ jobs:
- name: Build C++ demos
run: |
make -C build demo_diversity_result_handler
linux-x86_64-warnings-cmake:
name: Linux x86_64 warnings (cmake)
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Build and Test with warnings (cmake)
uses: ./.github/actions/build_cmake
with:
warning_level: '1'
warnings_as_errors: 'true'
upload_artifacts: 'false'
linux-x86_64-AVX2-cmake:
name: Linux x86_64 AVX2 (cmake)
needs: linux-x86_64-cmake
Expand Down
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ endif()

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

# Include compiler warnings configuration
include(StrictCompilerWarnings)
faiss_print_warning_config()

# Valid values are "generic", "avx2", "avx512", "avx512_spr", "sve".
option(FAISS_OPT_LEVEL "" "generic")
option(FAISS_ENABLE_GPU "Enable support for GPU indexes." ON)
Expand Down Expand Up @@ -119,7 +123,13 @@ endif()
include(CTest)
if(BUILD_TESTING)
add_subdirectory(tests)
add_subdirectory(perf_tests)
# perf_tests require gflags - make them optional
find_package(gflags QUIET)
if(gflags_FOUND)
add_subdirectory(perf_tests)
else()
message(STATUS "gflags not found, skipping perf_tests")
endif()
if(FAISS_ENABLE_GPU)
if(FAISS_ENABLE_ROCM)
add_subdirectory(faiss/gpu/test)
Expand Down
38 changes: 38 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,44 @@ outlined on that page and do not file a public issue.
* 80 character line length (both for C++ and Python)
* C++ language level: C++17

## Compiler Warnings

Faiss supports configurable compiler warning levels. We encourage contributors
to build with warnings enabled to catch potential issues early.

### Warning Levels

Configure the warning level with `-DFAISS_WARNING_LEVEL=<level>`:

| Level | Description | Recommended For |
|-------|-------------|-----------------|
| 0 | Disabled (default) | Normal builds |
| 1 | Basic warnings (`-Wall -Wextra`) | Development builds |
| 2 | Standard warnings (adds `-Wpedantic`, `-Wshadow`, etc.) | Code review |
| 3 | Strict warnings (all recommended warnings) | Static analysis |

### Example Usage

```bash
# Build with basic warnings
cmake .. -DFAISS_WARNING_LEVEL=1

# Build with warnings as errors (strict mode)
cmake .. -DFAISS_WARNING_LEVEL=2 -DFAISS_WARNINGS_AS_ERRORS=ON
```

### Best Practices for New Code

When writing new code:

* Use `static_cast<>`, `reinterpret_cast<>`, or `const_cast<>` instead of C-style casts
* Initialize all member variables in the member initializer list
* Order member initializers to match declaration order
* Use `[[maybe_unused]]` for intentionally unused parameters
* Use `override` for virtual function overrides
* Prefer `nullptr` over `NULL` or `0` for null pointers
* Avoid shadowing variables in inner scopes

## License

By contributing to Faiss, you agree that your contributions will be licensed
Expand Down
2 changes: 2 additions & 0 deletions benchs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ find_package(OpenMP REQUIRED)
add_executable(bench_ivf_selector EXCLUDE_FROM_ALL bench_ivf_selector.cpp)
target_link_libraries(bench_ivf_selector PRIVATE faiss_avx512 ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES} OpenMP::OpenMP_CXX)
target_compile_options(bench_ivf_selector PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-mavx2 -mfma -mf16c -mavx512f -mavx512cd -mavx512vl -mavx512dq -mavx512bw -mpopcnt>)
faiss_add_warnings(bench_ivf_selector)

add_executable(bench_result_handler_overhead EXCLUDE_FROM_ALL
bench_result_handler_overhead.cpp)
target_link_libraries(bench_result_handler_overhead PRIVATE faiss_avx512 ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES} OpenMP::OpenMP_CXX)
target_compile_options(bench_result_handler_overhead PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-mavx2 -mfma -mf16c -mavx512f -mavx512cd -mavx512vl -mavx512dq -mavx512bw -mpopcnt>)
faiss_add_warnings(bench_result_handler_overhead)
7 changes: 7 additions & 0 deletions c_api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ endif()

add_library(faiss_c_sve ${FAISS_C_SRC})
target_link_libraries(faiss_c_sve PRIVATE faiss_sve)

# Apply compiler warnings
faiss_add_warnings(faiss_c)
faiss_add_warnings(faiss_c_avx2)
faiss_add_warnings(faiss_c_avx512)
faiss_add_warnings(faiss_c_avx512_spr)
faiss_add_warnings(faiss_c_sve)
if(NOT FAISS_OPT_LEVEL STREQUAL "sve")
set_target_properties(faiss_c_sve PROPERTIES EXCLUDE_FROM_ALL TRUE)
endif()
Expand Down
2 changes: 1 addition & 1 deletion c_api/impl/AuxIndexStructures_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ int faiss_RangeSearchPartialResult_new_result(
idx_t qno,
FaissRangeQueryResult** qr) {
try {
auto q = &reinterpret_cast<RangeSearchPartialResult*>(res)->new_result(
auto& q = reinterpret_cast<RangeSearchPartialResult*>(res)->new_result(
qno);
if (qr) {
*qr = reinterpret_cast<FaissRangeQueryResult*>(&q);
Expand Down
114 changes: 114 additions & 0 deletions cmake/StrictCompilerWarnings.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# Strict compiler warnings module for code quality enforcement
# This module provides functions to enable compiler warnings at various levels

# Warning levels:
# 0 = Disabled (default for normal builds)
# 1 = Basic warnings (Wall, Wextra) - Recommended minimum
# 2 = Standard warnings (adds Wpedantic, Wshadow, etc.)
# 3 = Strict warnings (all recommended warnings)
set(FAISS_WARNING_LEVEL "0" CACHE STRING "Warning level: 0=off, 1=basic, 2=standard, 3=strict")
set_property(CACHE FAISS_WARNING_LEVEL PROPERTY STRINGS "0" "1" "2" "3")

option(FAISS_WARNINGS_AS_ERRORS "Treat warnings as errors (-Werror)" OFF)

# Function to apply warnings to a target based on the warning level
function(faiss_add_warnings target)
if(FAISS_WARNING_LEVEL EQUAL 0)
return()
endif()

# Level 1: Basic warnings
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:
-Wall
-Wextra
>
)
elseif(MSVC)
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:/W3>
)
endif()

# Level 2: Standard warnings
if(FAISS_WARNING_LEVEL GREATER_EQUAL 2)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:
-Wpedantic
-Wshadow
-Wnon-virtual-dtor
-Wunused
-Woverloaded-virtual
-Wformat=2
-Wimplicit-fallthrough
>
)
elseif(MSVC)
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:/W4 /permissive->
)
endif()
endif()

# Level 3: Strict warnings
if(FAISS_WARNING_LEVEL GREATER_EQUAL 3)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:
-Wold-style-cast
-Wcast-align
-Wconversion
-Wsign-conversion
-Wnull-dereference
-Wdouble-promotion
-Wmisleading-indentation
-Wduplicated-cond
-Wduplicated-branches
-Wlogical-op
-Wuseless-cast
>
)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:
-Wold-style-cast
-Wcast-align
-Wconversion
-Wsign-conversion
-Wnull-dereference
-Wdouble-promotion
>
)
endif()
endif()

# Warnings as errors
if(FAISS_WARNINGS_AS_ERRORS)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:-Werror>
)
elseif(MSVC)
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:/WX>
)
endif()
endif()
endfunction()

# Print warning configuration
function(faiss_print_warning_config)
if(FAISS_WARNING_LEVEL GREATER 0)
message(STATUS "FAISS Warning Level: ${FAISS_WARNING_LEVEL}")
if(FAISS_WARNINGS_AS_ERRORS)
message(STATUS "FAISS Warnings as Errors: ON")
endif()
endif()
endfunction()
8 changes: 8 additions & 0 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ target_link_libraries(demo_weighted_kmeans PRIVATE faiss)
add_executable(demo_residual_quantizer EXCLUDE_FROM_ALL demo_residual_quantizer.cpp)
target_link_libraries(demo_residual_quantizer PRIVATE faiss)

faiss_add_warnings(demo_imi_flat)
faiss_add_warnings(demo_imi_pq)
faiss_add_warnings(demo_ivfpq_indexing)
faiss_add_warnings(demo_nndescent)
faiss_add_warnings(demo_sift1M)
faiss_add_warnings(demo_weighted_kmeans)
faiss_add_warnings(demo_residual_quantizer)

add_subdirectory(diversity_filter)
1 change: 1 addition & 0 deletions demos/diversity_filter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
add_executable(demo_diversity_result_handler EXCLUDE_FROM_ALL
demo_diversity_result_handler.cpp)
target_link_libraries(demo_diversity_result_handler PRIVATE faiss)
faiss_add_warnings(demo_diversity_result_handler)
Loading
Loading